Explaining "this" Keyword in Java with an Example

  • Java
  • Thread starter pairofstrings
  • Start date
  • Tags
    Java
In summary, the 'this' keyword in Java refers to the current object and is commonly used in constructor functions to distinguish between local variables and instance variables. It is also used to call the basic constructor in case of multiple constructors. An object is an instance of a class, and a variable is a named "bucket" that holds a value. Instance variables are unique to each object, while local variables are specific to a function. The use of 'this' makes it clear which variables belong to the class and which are local to the function.
  • #1
pairofstrings
411
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
  • #2
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:
  • #3
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:

1. What is the purpose of the "this" keyword in Java?

The "this" keyword in Java is used to refer to the current object within a class. It is used to access the instance variables and methods of the current object.

2. What is the difference between "this" and "super" keywords in Java?

The "this" keyword refers to the current object, while the "super" keyword refers to the parent class of the current object. "this" is used to access the current class's members, while "super" is used to access the parent class's members.

3. Can "this" keyword be used in a static method in Java?

No, the "this" keyword cannot be used in a static method in Java as it refers to the current object and static methods do not have a current object.

4. How is "this" keyword used in constructors in Java?

In constructors, the "this" keyword is used to call one constructor from another constructor in the same class. It is also used to pass arguments to the constructor or to initialize instance variables.

5. Can "this" keyword be used in a method of a different class in Java?

No, the "this" keyword can only be used within the same class to refer to the current object. It cannot be used in a method of a different class.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
  • Introductory Physics Homework Help
Replies
5
Views
794
Replies
13
Views
736
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top