What is this Keyword in Java?

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 form attributes and parameters that have same names.

Post-obit are various uses of 'this' keyword in Java:

  • It can be used to refer instance variable of current form
  • It can be used to invoke or initiate electric current class constructor
  • It tin can be passed as an argument in the method telephone call
  • It can be passed as argument in the constructor call
  • It can exist used to return the electric current class case

Click here if the video is non accessible

Empathize 'this' keyword with an instance.

Java this keyword example
Java this keyword Example
  1. Form: class Account
  2. Instance Variable: a and b
  3. Method Prepare data: To fix the value for a and b.
  4. Method Evidence data: To display the values for a and b.
  5. Main method: where we create an object for Account form 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 iii respectively.

But the value is 0, Why? Let investigate.

this Keyword in Java

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

this Keyword in Java

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, information technology does not prepare the value of 'a' when the method set data is called.

this Keyword in Java

The solution is the "this" keyword

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

this Keyword in Java

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

this Keyword in Java

So now the compiler knows,

  • The 'a' on the left-hand side is an Example 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 plenty to choose unlike names for your example variable and methods arguments.

Java THIS Keyword

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

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

Java THIS Keyword

Well, the compiler implicitly appends the example variable with "this" keyword (paradigm beneath).

Java THIS Keyword

Such that when object 1 is calling the ready data method, an instance variable is appended by its reference variable.

this Keyword in Java

While object ii is calling the set up information method, an instance variable of object 2 is modified.

this Keyword in Java

This process is taken care by the compiler itself. You don't take to append 'this' keyword explicitly unless there is an exceptional state of affairs as in our instance.

Example: To learn the utilize "this" keyword

Step 1) Re-create 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(){    Organization.out.println("Value of A ="+a);    Arrangement.out.println("Value of B ="+b);  }  public static void main(Cord args[]){    Account obj = new Account();    obj.setData(2,3);    obj.showData();  } }

Stride 2) Save ,Compile & Run the code.

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

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

Pace iv) Save, Compile, and Run the code. This time effectually, values of a & b are fix to 2 & 3 respectively.

Summary

  • this Keyword in Coffee is a reference variable that refers to the electric current object.
  • One of the use of this keyword in Java is to refer current class example variable
  • It can exist used to invoke or initiate current course constructor
  • It tin exist passed as an statement in the method call
  • this pointer in Java can be passed every bit argument in the constructor phone call
  • this operator in Java can exist used to render the current class instance
  • this in Coffee 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.

Check our commodity on Java Interview Questions:- Click here