Java Explaining "this" Keyword in Java with an Example

  • Thread starter Thread starter pairofstrings
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The discussion clarifies the use of the 'this' keyword in Java, particularly in constructors, to distinguish between class instance variables and method parameters. It explains that 'this' refers to the current object of the class, allowing access to instance variables when local variables have the same name. An example is provided where a Box class is instantiated with different dimensions, demonstrating how each object maintains its own instance variables. The distinction between objects, variables, and instance variables is emphasized, with objects being instances of classes and variables being named containers for values. Overall, the 'this' keyword enhances code clarity by explicitly indicating which variables belong to the class.
pairofstrings
Messages
411
Reaction score
7
Hello everyone. I want explanation of the working of 'this' keyword in java. I read articles but couldn't understand. Please help me here. I don't understand what objects and variables they are talking about. Also give me an example of what object, variable, instance variable is.

Box(double w, double h, double d) {
this.width = w;
this.height = h;
this.depth = d;

In the above example, is width height depth an object or variable?? Explain me the 'this' keyword here please.
 
Technology news on Phys.org
Writing code like that is a common convention especially for constructor functions.

Actually your example is a bit silly. A better example would be
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;

In that example there are two different variables called "width". One of them will have been declared in the class. The other one is the parameter to the constructor.

Within the constructor, the parameters are local variables and therefore the parameter "width" "hides" the class variable with the same name. "width" refers to the parameter, and "this.width" refers to the class variable. "this" just means "the name of the object of type Box that this function call refers to".

Your original example would work just as well without using "this":

Box(double w, double h, double d) {
width = w;
height = h;
depth = d;

because all the variable names are unique anyway. But using "this" makes it clear which variables belong to the class and which are local to the function.
 
Last edited:
An object is an instance of a class. A variable is a general term for a named "bucket" that you can keep a value inside (int number, float precise_number, String mySentence - these are all variables, and they all have a different type which is important, so the computer has some idea of what kind of variable you are talking about. Words are treated differently from numbers.) An instance variable, ok so you have a class (a template), and from this class you instantiate objects (you say, computer take this class design (of a box say), and build me an object (a box). Build as many boxes as you want. Box1, Box2, Box3 - all box objects created from the Box class. Each Box object has it's own copies of the variables that make up a box (width, height, depth). If you change one, you don't affect the other Boxes, because they are instance variables, related to the particular instance (Box1, Box2, Box3) of the class (Box)

Here's a small example:

Code:
public class Box
{
    private:
    int width, height, depth;

    public Box(int width, height, depth)
    {
        this.width = width;
        this.height = height;
        this.depth = depth;
    }

    int getVolume()
    {
        volume = width * height * depth;
        return volume;
    }

    public static void main()
    {
        Box Box1 = new Box(10, 10, 10);
        Box Box2 = new Box(20, 20, 20);
        Box Box3 = new Box(30, 30, 30);

        int Vol1 = Box1.getVolume();
        int Vol2 = Box2.getVolume();
    }
}

Here I declare a class called Box. It contains a constructor, which is a special function to initialize the variables when you instantiate the class. What does that mean? In the main() function, refer to the line that says "Box Box1 = new Box(10, 10, 10);" This is an example of creating an object from a class definition, or instantiating the class, or creating an instance of the class. Before this, your class is just a template (a box design). After you create an object with the keyword new you have something useable, an actual Box.

You can see that we call getVolume() on two different instances of the Box class (Box1, Box2). They each have different width, height and depth, hence they will return a different volume, because their instance variables contain different values. Same class, different object (or instance).

The keyword this is a way of referring to the current object. See this page for a pretty good explanation with an example:

http://download.oracle.com/javase/tutorial/java/javaOO/thiskey.html

You can also use it to call the basic constructor if you have multiple (overloaded) constructors. The keyword super is closely related to this, it refers to the super-class, or parent-class, you might not be up to that just yet.

Excuse my Java if I made any glaring mistakes, it's been a while. :P
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
1
Views
2K
Replies
1
Views
3K
Replies
4
Views
6K
Replies
4
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
2
Views
13K
Replies
1
Views
2K
Back
Top