this keyword in java
There can be a lot of usage of java this keyword. In java, this is a reference variable that refers to the current object.
Usage of java this keyword
Here is given the 6 usage of java this keyword.
- this keyword can be used to refer current class instance variable.
- this() can be used to invoke current class constructor.
- this keyword can be used to invoke current class method (implicitly)
- this can be passed as an argument in the method call.
- this can be passed as argument in the constructor call.
- this keyword can also be used to return the current class instance.
Suggestion: If you are beginner to java, lookup only two usage of this keyword.
1) The this keyword can be used to refer current class instance variable.
If there is ambiguity between the instance variable and parameter, this keyword resolves the problem of ambiguity. |
Understanding the problem without this keyword
Let's understand the problem if we don't use this keyword by the example given below: |
Test it Now
In the above example, parameter (formal arguments) and instance variables are same that is why we are using this keyword to distinguish between local variable and instance variable. |
Solution of the above problem by this keyword
Test it Now
Output111 Karan
222 Aryan
If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program: |
Program where this keyword is not required
Test it Now
Output:111 Karan
222 Aryan
2) this() can be used to invoked current class constructor.
The this() constructor call can be used to invoke the current class constructor (constructor chaining). This approach is better if you have many constructors in the class and want to reuse that constructor.
Test it Now
Output:
default constructor is invoked
default constructor is invoked
111 Karan
222 Aryan
Where to use this() constructor call?
The this() constructor call should be used to reuse the constructor in the constructor. It maintains the chain between the constructors i.e. it is used for constructor chaining. Let's see the example given below that displays the actual use of this keyword. |
Test it Now
Output:111 Karan null
222 Aryan delhi
Rule: Call to this() must be the first statement in constructor.
Test it Now
Output:Compile Time Error
3)The this keyword can be used to invoke current class method (implicitly).
You may invoke the method of the current class by using the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method. Let's see the example |
Test it Now
4) this keyword can be passed as an argument in the method.
The this keyword can also be passed as an argument in the method. It is mainly used in the event handling. Let's see the example: |
Test it Now
Application of this that can be passed as an argument:
In event handling (or) in a situation where we have to provide reference of a class to another one. |
5) The this keyword can be passed as argument in the constructor call.
We can pass the this keyword in the constructor also. It is useful if we have to use one object in multiple classes. Let's see the example: |
Test it Now
6) The this keyword can be used to return current class instance.
We can return the this keyword as an statement from the method. In such case, return type of the method must be the class type (non-primitive). Let's see the example: |
Syntax of this that can be returned as a statement
Example of this keyword that you return as a statement from the method
Test it Now
Proving this keyword
Let's prove that this keyword refers to the current class instance variable. In this program, we are printing the reference variable and this, output of both variables are same. |
Test it Now
Output:A5@22b3ea59
A5@22b3ea59
|