# This keyword in Java

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.

AlephZero
Homework Helper
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: