This keyword in java

Let’s learn this keyword in java.

this keyword in java

this keyword in java

Java this keyword is used to refer instance variables to the current object. “this” acts as reference to current object where constructor is invoked.

For example,

class Bank
{
   // instance variable
   int m;
   int n;
 
   public void setValue(int m, int n)
   {
      m = m;
      n = n;
   }
 
   public void showValue()
   {
      System.out.println("Value m = " + m);
      System.out.println("Value n = " + n);
   }
}

public class ThisKeywordDemo
{
   public static void main(String[] args)
   {
      Bank obj = new Bank();
      obj.setValue(5,6);
      obj.showValue();
   }
}



In the above example I have created class “Bank” with instance variables “m” and “n” and setValue() method to set data.

To display values for “m” and “n” we have created method showValue().  When you run the above program you will get below output,

Value m = 0

Value n = 0

Our expected output for “m” and “n” should be 5 and 6. But the value is zero!!!??

Also read – constructor in java

Let’s analyse,

In the method setValue() the arguments are declared with “m” and “n”. While instance variables are also named with “m” and “n”.

During execution, compiler is confused whether “m” on the left side of the assignment operator is the instance variable or the local variable.

Hence it does not set the value of “m” and “n” when setValue() method is called. This problem can be solved by using “this” keyword. Here’s the java program,

class Bank
{ 
   int m;
   int n;
 
   public void setValue(int m, int n)
   {
      // java this keyword
      this.m = m;
      this.n = n;
   }
 
   public void showValue()
   {
      System.out.println("Value m = " + m);
      System.out.println("Value n = " + n);
   }
}

public class ThisExample
{
   public static void main(String[] args)
   {
      Bank obj = new Bank();
      obj.setValue(5,6);
      obj.showValue();
   }
}



In method setValue() we should append both “m” and “n” with “this” keyword followed by dot operator and instance variable.

Also read – operators in java

During code execution when object calls method setValue(), keyword “this” is replaced by the object’s handler ‘obj’ like this,

obj.m = m;

obj.n = n;

Now the compiler knows that “m” on the left hand side is the instance variable whereas “m” on the right hand side is the local variable.

The variables are initialized correctly and expected output is shown as below.

Value m = 5

Value n = 6

Suppose, say, if user chooses different variable names for instance variable and method arguments like this,

class Bank
{
   int m;
   int n;
   public void setValue(int a, int b)
   {
      // no need of "this" keyword here
      // because instance variable and method arguments are different
      m = a; 
      n = b;
   }
   public void showValue()
   {
      System.out.println("Value m = " + m);
      System.out.println("Value n = " + n);
   }
}



In this case we must create two objects of class Bank, each calling setValue() and showValue() method like this,

public static void main(String[] args)
{ 
   Bank obj1 = new Bank();
   obj1.setValue(8,6);
   obj1.showValue();
   Bank obj2 = new Bank();
   obj2.setValue(2,6);
   obj2.showValue();
}



Now how will the compiler decide whether it is supposed to work on the instance variable of “obj1” or “obj2”?

Well compiler implicitly appends the instance variable with “this” keyword.

this.m = a;

this.n = b;

Here when “obj1” calls method setValue(), instance variable is appended by its reference variable. Similarly for “obj2”.

obj1.m = a;

obj1.n = b;

obj2.m = a;

obj2.n = b;

This process is taken care by compiler and you do not need to append “this” keyword explicitly unless instance variable name and local variable name are same.


‘this’ reference in java

Now let’s learn what is the use of “this” keyword in instance variables, class constructor, current class instance, method parameter and class method.

referring current class instance variables

class ThisDemo 
{
   int i; 
   int j; 

   // parameterized constructor 
   ThisDemo(int a, int b) 
   { 
      this.i = a; 
      this.j = b; 
   } 

   void print() 
   { 
      // printing variables i and j 
      System.out.println("i = " + i + " j = " + j); 
   } 

   public static void main(String[] args) 
   { 
      ThisDemo obj = new ThisDemo(15, 45);
      obj.print(); 
   }
}



Output:

i = 15 j = 45


argument in constructor call

class X 
{
   Y obj;
   // parameterized constructor with object Y as parameter 
   X(Y obj) 
   { 
      this.obj = obj; 
      // calling print method of class Y 
      obj.print(); 
   }
}

class Y 
{
   int a = 45;
   // default constructor that create a object of X 
   // passing "this" as argument in constructor
   Y()
   { 
      X obj = new X(this); 
   }

   // showing value of "a" 
   void print() 
   {
      System.out.println("Value of a in class Y : " + a); 
   } 

   public static void main(String[] args) 
   { 
      Y obj = new Y(); 
   }
}



Output:

Value of a in class Y : 45


invoking current class method

class ThisDemo 
{
   void print() 
   { 
      // calling display() 
      this.display();
      System.out.println("in print method"); 
   }

   void display() 
   { 
      System.out.println("in display method"); 
   }

   public static void main(String args[]) 
   { 
      ThisDemo obj = new ThisDemo(); 
      obj.print(); 
   }
}



Output:

in display method
in print method


“this” keyword as method parameter

class ThisDemo 
{
   int x; 
   int y; 

   // default constructor 
   ThisDemo() 
   { 
      x = 62; 
      y = 65; 
   }

   // method accepts 'this' keyword as parameter 
   void print(ThisDemo obj)
   { 
      System.out.println("X = " + x + " Y = " + y); 
   } 

   // method returning current class instance 
   void get() 
   { 
      print(this); 
   } 

   public static void main(String[] args) 
   { 
      ThisDemo obj1 = new ThisDemo(); 
      obj1.get(); 
   }
}



Output:

X = 62 Y = 65


return current class instance

class ThisDemo 
{
   int x; 
   int y; 

   // default constructor 
   ThisDemo() 
   { 
      x = 25; 
      y = 26; 
   } 

   // method returning current class instance 
   ThisDemo get()
   { 
      return this; 
   }

   // printing value of variables x and y 
   void print() 
   { 
      System.out.println("X = " + x + " Y = " + y); 
   } 

   public static void main(String[] args) 
   { 
      ThisDemo object = new ThisDemo(); 
      object.get().print(); 
   }
}



Output:

X = 25 Y = 26


invoking current class constructor

class ThisDemo 
{
   int x; 
   int y; 

   // default constructor 
   ThisDemo() 
   { 
      this(51, 65); 
      System.out.println("in default constructor \n"); 
   } 

   // parameterized constructor 
   ThisDemo(int m, int n) 
   { 
      this.x = m; 
      this.y = n; 
      System.out.println("in parameterized constructor"); 
   } 

   public static void main(String[] args) 
   { 
      ThisDemo obj = new ThisDemo(); 
   }
}


Output:

in parameterized constructor
in default constructor