this Keyword in Java
โก Smart Summary
Java this Keyword is a reference variable that points to the current object inside a method or constructor. Lessons cover purpose, common uses, naming conflicts, constructor chaining, and a worked example.

What is this Keyword in Java?
The this keyword in Java is a reference variable that refers to the current object of a method or a constructor. The main purpose of using this keyword in Java is to remove the confusion between class attributes and parameters that have the same name.
Use of this Keyword in Java
Following are various uses of the this keyword in Java:
- It can be used to refer to the instance variable of the current class.
- It can be used to invoke or initiate the current class constructor.
- It can be passed as an argument in a method call.
- It can be passed as an argument in a constructor call.
- It can be used to return the current class instance.
Click here if the video is not accessible
How to Understand the this Keyword With an Example

- Class: class Account
- Instance Variable: a and b
- Method Set data: To set the value for a and b.
- Method Show data: To display the values for a and b.
- Main method: where we create an object for the Account class and call methods set data and show data.
Walking Through the Compiler Confusion
Let us compile and run the code.
Our expected output for A and B should be initialized to the values 2 and 3 respectively.
But the value is 0. Why? Let us investigate.
In the method Set data, the arguments are declared as a and b, while the instance variables are also named a and b.
During execution, the compiler is confused. Whether “a” on the left side of the assigned operator is the instance variable or the local variable. Hence, it does not set the value of “a” when the method set data is called.
The solution is the this keyword.
Append both “a” and “b” with the Java this keyword followed by a dot (.) operator.
During code execution when an object calls the method setdata, the keyword this is replaced by the object handler “obj.” (See the image below.)
So now the compiler knows,
- The “a” on the left-hand side is an instance variable.
- Whereas the “a” on the right-hand side is a local variable.
The variables are initialized correctly, and the expected output is shown.
How the Compiler Handles Multiple Objects
Suppose you choose different names for your instance variable and method arguments.
But this time around, you create two objects of the class, each calling the set data method.
How will the compiler determine whether it is supposed to work on the instance variable of object 1 or object 2?
Well, the compiler implicitly appends the instance variable with the this keyword (image below).
Such that when object 1 is calling the set data method, an instance variable is appended by its reference variable.
While object 2 is calling the set data method, an instance variable of object 2 is modified.
The compiler handles this automatically. You do not have to append the this keyword explicitly unless an exceptional situation requires it.
Example: this Keyword in Java With Code
Example: To learn the use of the this keyword.
Step 1) Copy the following code into a notepad.
class Account{ int a; int b; public void setData(int a ,int b){ a = a; b = b; } public void showData(){ System.out.println("Value of A ="+a); System.out.println("Value of B ="+b); } public static void main(String args[]){ Account obj = new Account(); obj.setData(2,3); obj.showData(); } }
Step 2) Save, compile, and run the code.
Step 3) Value of a and b is shown as zero? To correct the error, append line # 6 and 7 with the this keyword.
this.a =a; this.b =b;
Step 4) Save, compile, and run the code. This time around, the values of a and b are set to 2 and 3 respectively.
FAQs
Check our article on Java Interview Questions:- Click here











