What is this Keyword in Coffee?

this keyword in Coffee is a reference variable that refers to the electric current object of a method or a constructor. The principal purpose of using this keyword in Java is to remove the defoliation between form attributes and parameters that take same names.

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

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

Click hither if the video is not accessible

Sympathize 'this' keyword with an case.

Java this keyword example
Java this keyword Case
  1. Class: class Account
  2. Case Variable: a and b
  3. Method Ready data: To ready the value for a and b.
  4. Method Show data: To display the values for a and b.
  5. Main method: where nosotros create an object for Account course and call methods set information and prove data.

Let's compile and run the code

Our expected output for A and B should exist initialized to the values 2 and 3 respectively.

Just the value is 0, Why? Allow investigate.

this Keyword in Java

In the method Set data, the arguments are declared as a and b, while the example variables are as well 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 set the value of 'a' when the method ready information is called.

this Keyword in Java

The solution is the "this" keyword

Append 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 past the object handler "obj." (See the image beneath).

this Keyword in Java

And then now the compiler knows,

  • The 'a' on the left-paw 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 lot are smart enough to choose different names for your case variable and methods arguments.

Java THIS Keyword

Merely this time effectually, yous create two objects of the form, each calling the set data method.

How the compiler will make up one's mind whether it is supposed to work on instance variable of object ane or object 2.

Java THIS Keyword

Well, the compiler implicitly appends the instance variable with "this" keyword (image beneath).

Java THIS Keyword

Such that when object one is calling the set up data method, an instance variable is appended by its reference variable.

this Keyword in Java

While object 2 is calling the set data method, an example variable of object two is modified.

this Keyword in Java

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

Instance: To learn the use "this" keyword

Pace one) Copy the following code into a notepad.

form 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 chief(String args[]){    Account obj = new Account();    obj.setData(2,iii);    obj.showData();  } }

Step 2) Save ,Compile & Run the code.

Step three) Value of a & b is shown as nada? To correct the error suspend line # 6 & vii with "this" keyword.

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

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

Summary

  • this Keyword in Java is a reference variable that refers to the current object.
  • One of the utilize of this keyword in Java is to refer electric current form case variable
  • It can exist used to invoke or initiate electric current class constructor
  • It can be passed equally an argument in the method call
  • this pointer in Java can be passed as statement in the constructor call
  • this operator in Java can exist used to return the current course instance
  • this in Java is a reference to the current object, whose method is existence chosen upon.
  • Y'all can use "this" keyword to avoid naming conflicts in the method/constructor of your instance/object.

Check our commodity on Java Interview Questions:- Click hither