Now the question is how we avoid this problem and differentiate between the instance variables, local variables, constructors, and classes with the same name? The answer is very simple; we can solve it with the help of the “this” keyword.
This post explains the functionality of the “this” keyword in Java.
What does this mean in Java?
In Java, this keyword is represented as a reference variable that refers to an object. The “this” keyword eliminates the confusion for the compiler due to which it can easily differentiate between an instance and the local variable.
Let’s head over to the following example for more details.
Code:
int h;
void val(int h)
{
h = h;
}
void disp()
{
System.out.println(h);
}
public static void main(String[] args)
{
arry tcheck = new arry();
tcheck.val(20);
tcheck.disp();
}
}
In the above code, we give the same name to an instance variable and a local variable. Then the value of the local variable is assigned to the instance variable. Lastly, we display the value with the help of the disp() method.
Output:
In this output, you can see that the compiler gets confused with the same instance and local variable names and is unable to differentiate between them. So it returns 0 as an output; now, let’s use “this” keyword in the above example to see the difference.
Code:
int h;
void val(int h)
{
this.h = h;
}
void disp()
{
System.out.println(h);
}
public static void main(String[] args)
{
arry tcheck = new arry();
tcheck.val(20);
tcheck.disp();
}
}
In the above code, we assign the value of the local variable to an instance variable using the “this” keyword. Lastly, we display the value with the help of the disp() method.
Output:
Now you see the difference; after using “this” keyword, the compiler can differentiate between local and instance variables with the same name and assign the value of the local variable to the instance variable. Lastly, we get the required result which is 20.
Use of this keyword to call the current class method
We can also use this keyword to implicitly call the current class method, which means if we have created two methods in a class, then we can call one method by using the second method. Let’s see the following practical example.
Code:
void val()
{
System.out.println("Current class methods is called by using this keyword.");
}
void disp()
{
this.val();
}
public static void main(String[] args)
{
arry tcheck = new arry();
tcheck.disp();
}
}
In this code, we create two methods val() and disp(). Then we request to print a message in the val() method and call the val() method in the disp() method by using this keyword.
Output:
The output shows that one method is successfully called by the second method using this keyword.
Use of this keyword with constructors
We can also use this keyword to call one constructor in another constructor. In the following practical example, we have used this keyword with a constructor for your better understanding.
Code:
int m =3, r, x=6;
arry()
{
r = m+x;
}
arry(int f)
{
this();
System.out.println(m+" + "+x+" = "+r);
}
public static void main(String[] args)
{
arry tcheck = new arry(20);
}
}
In this code we create two constructors. One is a parameterized constructor and the other is non parameterized. Then we call non parameterized constructor in parameterized constructor using this keyword.
Output:
The output shows that this keyword successfully calls the first constructor in the second constructor.
Use this keyword as an argument
In Java, we can also pass this keyword as an argument in a method. The following practical example gives you a satisfactory understanding.
Code:
int m =3, r, x=6;
void val(arry tcheck)
{
r = m+x;
System.out.println(m+" + "+x+" = "+r);
}
void disp()
{
val(this);
}
public static void main(String[] args)
{
arry tcheck = new arry();
tcheck.disp();
}
}
In this code, we create two methods val() and disp(). Then we pass the object of the arry class as a parameter to the val() method. At the time of the method call, this keyword is passed to the disp() method as an argument.
Output:
In this code, we have successfully passed this keyword as an argument and got the required output.
By going through the above examples, you have learned to use this keyword in various scenarios.
Conclusion
In Java, the “this” keyword is used as a reference variable for current class objects. It quashes the confusion between classes, constructors, and instance variables with the same name. In this article, we have learned about the “this” keyword in detail, with examples and problems if you do not use the “this” keyword.