Java this keyword
this is a reference variable in Java. It refers to the current object. It can be used inside the method or constructor of the class. this can be used to refer current object from within an instance method or constructor.
this can be used in many contexts like-
- Invoke current class constructor
- Invoke current class method
- Return the current class object
- Pass an argument in the constructor call.
- Pass an argument in the method call.
Another use of this keyword is that it is not allowed two or more variable have the same name inside a class scope. Here we can see that the most common use of this keyword is to eliminate the confusion between class attributes and parameters with the same name.
Let’s have a look at below example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class ThisExample{ int a1; // Constructor with a parameter public ThisExample(int a1) { this.a1 = a1; } // Call the constructor public static void main(String[] args) { ThisExample obj = new ThisExample(5); System.out.println("a1 = " + obj.a1); } } |
Output
1 2 3 |
a1 = 5 |
We had used this keyword in the given example and we got output as a=5. If we omit the keyword in the given example then output will be a=0.
See the below example without this keyword.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class ThisExample{ int a1; // Constructor with a parameter public ThisExample(int a1) { a1 = a1; } // Call the constructor public static void main(String[] args) { ThisExample obj = new ThisExample(5); System.out.println("a1= " + obj.a1); } } |
Output
1 2 3 |
a1 = 0 |
‘this’ keyword in a different context
this with constructor
In the given example, ‘this’ is used to invoke the current class constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class ThisExample1 { int a; int b; //Default constructor ThisExample1() { this(10, 20); System.out.println("Inside the default constructor\n"); } //Parameterized constructor ThisExample1(int a, int b) { this.a = a; this.b = b; System.out.println("Inside the parameterized constructor"); } public static void main(String[] args) { ThisExample1 obj = new ThisExample1(); } } |
Output
1 2 3 4 |
Inside the parameterized constructor Inside the default constructor |
Using this to return the current class instance
In this example, this keyword will use to return the current class instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class ThisExample2 { int a; int b; //Default constructor ThisExample2() { a = 20; b = 25; } //method that return the class instance ThisExample2 get() { return this; } //Displaying value of variables a and b void display() { System.out.println("a = " + a + " b = " + b); } public static void main(String[] args) { ThisExample2 object = new ThisExample2(); object.get().display(); } } |
Output-
1 2 3 |
a = 20 b = 25 |
Using this as a method parameter-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class ThisExample3 { public static void main(String[] args) { ThisExample4 obj = new ThisExample4(); obj.i = 30; obj.method(); } } class ThisExample4 extends ThisExample3{ int i; void method() { method1(this); } void method1(ThisExample4 t) { System.out.println(t.i); } } |
Output-
1 2 3 |
30 |
Using this to invoke current class method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class ThisExample5{ void display() { // calling fuction show() this.show(); System.out.println("Inside display function"); } void show() { System.out.println("Inside show function"); } public static void main(String args[]) { ThisExample5 t1 = new ThisExample5(); t1.display(); } } |
Output-
1 2 3 4 |
Inside show function Inside display function |
Variable hiding in java with ‘this’ keyword
this keyword can be useful for variable hiding. In Java, we cannot take the same name for two instances; however, it is possible to take a local variable, instance variable, and mother parameter with the same name. In this process, the local variable hides the instance variable; this is called variable hiding.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class ThisExample6{ int var= 10; public static void main(String args[]) { ThisExample6 obj = new ThisExample6(); obj.method(20); obj.method(); } void method(int var) { var = 10; System.out.println(" Val of Instance variable :" + this.var); System.out.println("Val of Local variable :" + var); } void method() { int var = 40; System.out.println(" Val of Instance variable :" + this.var); System.out.println("Val of Local variable :" + var); } } |
Output-
1 2 3 4 5 6 |
Val of Instance variable: 10 Val of Local variable: 10 Val of Instance variable: 10 Val of Local variable: 40 |