In Java, this keyword is used to refer to the current class object. There are various situations where sometimes is important to mention the object that actually invoked a particular method, constructor and initialization of instance variable. ‘ This keyword’ allows us to provide a reference to the current object.
It resolves the problem of hiding of instance variable. In Java, when instance variable (Declared within a class) and local variable or method argument (Declared within a function including its argument ) have the same name in the same scope, then within a function local variable is referred over instance variable that hides its instance variable existence. To overcome the ambiguity between these variables, ‘this keyword’ is used.
Problem:
class sample{ int x; int y; sample(int x, int y) { x=x; y=y; } void display() { System.out.println(" Value of x = "+x+" Value of Y = "+y); } } public class ThisKeyword { public static void main(String[] args) { sample obj = new sample(9,10); obj.display(); } }
Output:
Value of x = 0 Value of Y = 0
Explanation:
Look at arguments of the constructor of the sample class, it has arguments with the same name as the instance variable. Within this sample constructor, the names provided in the arguments local to that particular block, as a result, there is ambiguity between instance variable and local variable. And we know Java compiler would hide the instance variables and give more priority to local ones. This is why the output would not give 9 and 10 instead, Java’s default constructor would provide them a default value that is 0.
Solution:
class sample{ int x; int y; sample(int x, int y) { this.x=x; // obj.x = x this.y=y; / y.obj = y } void display() { System.out.println(" Value of x = "+x+" Value of Y = "+y); } } public class ThisKeyword { public static void main(String[] args) { sample obj = new sample(9,10); obj.display(); } }
Output:
Value of x = 9 Value of Y = 10
So, This keyword clearly resolved the conflict between instance and local variable which simply depicts within that particular block obj.x = x; obj.y=y;
‘this keyword’ can be used to invoke overloaded constructors based on the arguments that it has. It helps in reducing the duplicate codes and forms a more structured program.
Problem:
class sample{ int x; int y; int z; sample() { sample(10,20); } sample(int a,int b) { sample(30); x=a; y=b; } sample(int c) { z=c; } void display() { System.out.println(" Value of x = "+x+" Value of Y = "+y+" Value of Z = "+z); } } public class ThisKeyword { public static void main(String[] args) { sample obj = new sample(); obj.display(); } }
Output:
Compile time error
Explanation:
Java compiler will give uncompilable code error and clearly, this type of declaration isn’t permitted.
Solution:
class sample{ int x; int y; int z; sample() { this(10,20); } sample(int a,int b) { this(30); x=a; y=b; } sample(int c) { z=c; } void display() { System.out.println(" Value of x = "+x+" Value of Y = "+y); } } public class ThisKeyword { public static void main(String[] args) { sample obj = new sample(); obj.display(); } }
Output:
Value of x = 10 Value of Y = 20 Value of Z = 30
It can be used to refer to the current class method. It might be useful when parent class and its subclass have the same method with same name and there may be a situation you want to call the current class’s method. However, we can simply call it by providing just the name of the method, regardless of unnecessarily putting ‘this keyword’ in it. For the sake of ‘this keyword’ usage, we mentioned this point. However, for “Super keyword” invoking parent class from the subclass method sounds more meaningful than this keyword.
Program:
class sample1{ int m; int n; sample1() { m=5; n=10; } void display() { System.out.println(" Value of M = "+m+" Value of N = "+n); } } class sample extends sample1{ int x; int y; sample(int a,int b) { x=a; y=b; } void callchecker() { this.display(); } @Override void display() { System.out.println(" Value of x = "+x+" Value of Y = "+y); } } public class ThisKeyword { public static void main(String[] args) { sample obj = new sample(10,20); obj.callchecker(); } }
Output:
Value of x = 10 Value of Y = 20
Note: we don’t need to put this keyword to call current class function. For example, this.display() can also be written as display()
We can pass a current class object as an argument in a function. But there is another way of passing it, that is using ‘this keyword’.
Situation:
class sample{ int x; int y; sample(int a,int b) { x=a; y=b; } void display(sample obj) { System.out.println(" Value of x = "+obj.x+" Value of Y = "+obj.y); } } public class ThisKeyword { public static void main(String[] args) { sample obj = new sample(10,20); obj.display(obj); } }
Output:
Value of x = 10 Value of Y = 20
Instead of passing an actual object we can alternately use ‘this keyword’ as an argument which would be a reference to the current object
Another way:
class sample{ int x; int y; sample(int a,int b) { x=a; y=b; } void checkme() { display(this); } void display(sample obj) { System.out.println(" Value of x = "+obj.x+" Value of Y = "+obj.y); } } public class ThisKeyword { public static void main(String[] args) { sample obj = new sample(10,20); obj.checkme(); } }
Output:
Value of x = 10 Value of Y = 20
It can be used to return the current object. It simply returns the instance of a class. So making any manipulation such as addition, subtraction and assigning to another object would also change the values of the current class object.
Program:
class sample{ int x; int y; sample(int a,int b) { x=a; y=b; } sample adding(sample obj) { this.x = 10 + obj.x; this.y = 10 + obj.y; return this; } void display() { System.out.println(" Value of x = "+x+" Value of Y = "+y); } } public class ThisKeyword { public static void main(String[] args) { sample obj = new sample(10,20); sample obj2; obj2 = obj.adding(obj); obj2.display(); } }
Output:
Value of x = 20 Value of Y = 30
Explanation:
We got ‘adding’ method that uses the values of the current class object’s variable and increments them by 10. However, unfortunately, it is not a good example, but at least it demonstrates which we expected. Note, All the manipulations have been done on the current object, resulting in changed values of obj itself and assigned it to obj2. so both obj and obj2 would give the same result.
This post was last modified on June 19, 2020