• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog

Keyword 'THIS' in Java is a reference variable that refers to the current object.

The various usage of keyword Java 'THIS' in Java is as per the below,

  • It can be used to refer current class instance variable
  • It can be used to invoke or initiate current class constructor
  • It can be passed as an argument in the method call
  • It can be passed as argument in the constructor call
  • It can be used to return the current class instance

Let's understand 'this' java keyword with an example.

Please be patient. The Video will load in some time. If you still face issue viewing video click here

Java THIS Keyword

  1. Class: Let's create a class Account with
  2. InstanceVariable: a and b and
  3. Method Set data: To set the value for a and b.
  4. Method Show data: To display the values for a and b.
  5. Main method: where we create an object for Account class and call methods set data and show data.

Let's 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's investigate.

Java THIS Keyword

In the method Set data, the arguments are declared as a and b, while the instance variables are also named as a and b.

Java THIS Keyword

During execution, the complier is confused. Whether "a" on the left side of the assigned operator is the instance variable or the local variable. Hence, it is not set the value of 'a' when the method set data is called.

Java THIS Keyword

So, problem can be overcome by "THIS" keyword

Append both 'a' and 'b' with the "this" keyword followed by a dot (.) operator.

Java THIS Keyword

Java THIS Keyword

During code execution when an object calls the method 'setdata'. The keyword 'this' is replaced by the object handler "obj." (See the image below).

Java THIS Keyword

So now the compiler know,

  • The 'a' on the left-hand side is an Instance variable.
  • Whereas the 'a' on right-hand side is a local variable

The variables are initialized correctly, and the expected output is shown.

Java THIS Keyword

Suppose you are smart enough to choose different names for your instance variable and methods arguments.

Java THIS Keyword

But this time around, you create two objects of the class, each calling the set data method.

How the compiler will determine whether it is supposed to work on instance variable of object 1 or object 2.

Java THIS Keyword

Well, the compiler implicitly appends the instance variable with "THIS" keyword (image below). Such that when object 1 is calling a set data method, instance variable are appended by its reference variable. While object 2 is calling the set data method, an instance variable of object 2 are modified.

Java THIS Keyword

Java THIS Keyword

Java THIS Keyword

This process is taken care by the compiler itself. You don't have to append 'this' keyword explicitly unless there is an exceptional situation as in our example.

Points to Remember:

  • "this" is a reference to the current object, whose method is being called upon.
  • You can use "this" keyword to avoid naming conflicts in the method/constructor of your instance/object.

Assignment: To learn the use "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 & Run the code.

Step 3 ) Value of a & b is shown as zero ? To correct the error append line # 6 & 7with "this" keyword.

this.a =a;
this.b=b;

Step 4) Save ,Compile & Run the code. This time around , values of a & b are set to 2 & 3 respectively.

Summary:

  • Keyword 'THIS' in Java is a reference variable that refers to the current object.
  • It can be used to refer current class instance variable
  • It can be used to invoke or initiate current class constructor
  • It can be passed as an argument in the method call
  • It can be passed as argument in the constructor call
  • It can be used to return the current class instance

 

YOU MIGHT LIKE: