Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

The this keyword

The this keyword is considered as one of the most important keywords in Java.

The this keyword is used to indicates the current object which has invoked the method or constructor.

To understand this keyword clearly, first let’s see a small example.

Example

class Car 
{
    String name;
    String color;
    int gears;
    boolean isAutoGear;
	
    Car(String name,String color,int gears,boolean isAutoGear)
    {
        name = name;
        color = color;
        gears = gears;
        isAutoGear = isAutoGear;
    }
	
    public void specifications()
    {
        System.out.println(name);
        System.out.println(color);
        System.out.println(gears);
        System.out.println(isAutoGear);
    }

    public static void main(String args[])
    {
        Car c = new Car("BMW-720","Black",6,true);
        c.specifications();
    }
}

Output

null
null
0
false

Now you might be surprised by looking at output and thinking how is this possible even though we have initialized the object using the parameterized constructor.
This is only because we have not initialized instance variables but local variables present inside the constructor. That is why none of the instance variables got initialized & the result is unexpected.
This happens because every local variable in every method or constructor hides/suppresses instance variables if they have the same name.
This condition is called instance variable hiding.

Now let’s see which are the instance variable and which are the local variables in the constructor used in above program so that you can understand the reason for above output.

Java Programming Language this Keyword 1

Note

Local variables are always dominant over other variables in their local area if they have the same name.

At last! Every dog is king in its local area.

Java Programming Language this Keyword 2

So how could we solve this problem?

There is one way to solve this problem. By writing local variables name different than instance variables name as shown below:

Example

class Car 
{
    String name;
    String color;
    int gears;
    boolean isAutoGear;
	
    Car(String n,String c,int g,boolean at)
    {
        name = n;
        color = c;
        gears = g;
        isAutoGear = at;
    }
	
    public void specifications()
    {
        System.out.println(name);
        System.out.println(color);
        System.out.println(gears);
        System.out.println(isAutoGear);
    }

    public static void main(String args[])
    {
        Car c = new Car("BMW-720","Black",6,true);
        c.specifications();
    }
}

Output

BMW-720
Black
6
true

Now we got expected output.
This happened because we used instance variables names & local variables names differently. So Java does not get confused between these names & there is no question of suppressing instance variables by local variables.
For better understanding look at the diagram shown below:

Java Programming Language this Keyword 3

But what if we want to initialize object through constructor using instance variables & local variables names same. And make Java recognize them separately without confusion.

There is the only one way to achieve this type of condition. That is by using this keyword.

The this keyword always refers the object on which the method or constructor is invoked.

Java Programming Language this Keyword 4

Therefore when we use this keyword with variables names in any method or a constructor, those variables are always considered as instance variables of the object on which the constructor or method is invoked.

Example

class Car 
{
    String name;
    String color;
    int gears;
    boolean isAutoGear;
	
    Car(String name,String color,int gears,boolean isAutoGear)
    {
        this.name = name;
        this.color = color;
        this.gears = gears;
        this.isAutoGear = isAutoGear;
    }
	
    public void specifications()
    {
        System.out.println(name);
        System.out.println(color);
        System.out.println(gears);
        System.out.println(isAutoGear);
    }

    public static void main(String args[])
    {
        Car c = new Car("BMW-720","Black",6,true);
        c.specifications();
    }
}

Output

BMW-720
Black
6
true

Here we get the expected output even though we have used same names for instance variables & local variables because variables used with this keyword are considered as instance variables of object c & those without this keyword are considered as local variables.

Java Programming Language this Keyword 5

One more example

class Car 
{
    String name;
    String color;
    int gears;
    boolean isAutoGear;
	
    Car(String name,String color,int gears,boolean isAutoGear)
    {
        this.name = name;
        this.color = color;
        this.gears = gears;
        this.isAutoGear = isAutoGear;
    }
	
    public void specifications()
    {
        System.out.println(name);
        System.out.println(color);
        System.out.println(gears);
        System.out.println(isAutoGear);
        System.out.println("-------------------");
    }

    public static void main(String args[])
    {
        Car c1 = new Car("BMW-720","Black",6,true);
        Car c2 = new Car("Ferrari","Red",7,true);
        c1.specifications();
        c2.specifications();
    }
}

Output

BMW-720
Black
6
true
-------------------
Ferrari
Red
7
true
-------------------

Java Programming Language this Keyword 6

Leave a reply