this keyword in Java

In this java programming tutorial, we will study and understand the working of this keyword in java. In simple terms – ‘this’ is a reference variable that refers to the current object. There are many different use cases of the this keyword in Java. Here are few use cases of the this keyword –

  • It can be used to refer current class instance variable.
  • this can be passed as an argument in the method call.
  • this keyword can be used to return current class instance.
  • this can be passed as argument in the constructor call.
  • this() : to invoke current class constructor.
1)  THIS keyword used to refer current class instance variable.

The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity. Lets take a program example –

this keyword program example

Output
Num value : 5
Program Explanation –

In this program writtern above, you can see that we have an instance variable of the class named num. Now in the parameter argument variable that we are passing in the setNum(int num) function is also named num. So in order to prevent ambiguity between which num is which variable, we use the this keyword. Thus the this.num is the instance variable and the num being passed as argument is simple an external variable value.

2)  THIS keyword passed as an argument in the method call.

The this keyword can also be passed as an argument in the method. It is mainly used in the event handling. Lets take a program example –

this keyword passed as an argument in the method call

Output
Function 1 invoked
Program Explanation –

In the above written program we are calling function1 by using function2. To do so, we are passing the same object reference using the this keyword. the this keyword in this case is actually referring to the current MyClass object.

3)  THIS keyword can be used to return current class instance.

We can return this keyword as an statement from the method. In such case, return type of the method must be the class type (non-primitive). Lets see a program example for the same –

this keyword used to return current class instance.

Output
a = 10 b = 20
Program Explanation –

In this program written above, we used this keyword to return the reference to the current object via the get() function and then accessed their instance variables.

4)  THIS keyword can be passed as argument in the constructor call.

Just like the this keyword can be used to refer current class instance variable in a method, it can also be used in a constructor to refer the instance variable. Let us see a program example –

this keyword passed as argument in the constructor call.

Output
Num value : 5
Program Explanation –

As per the program we are resolving the ambiguity between the instance variable num and the arguement variable num being passing in the parameterized constructor and the distinction is being done by the this keyword. Also the this keyword can work with user defined class objects too.

5)  this() keyword is used to invoke current class constructor.

The this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In other words, it is used for constructor chaining. Lets see a program example –

this keyword used to invoke current class constructor.

Output
111 ankit java null
112 sumit java 6000

Thus you can see that there are various applications and instances where the this keyword is used in Java.

Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *