Learn this keyword in java

Hello everyone!! Welcome to flower brackets blog. In this post we are going to learn this keyword in java.

“this” keyword in java is used to refer instance variables to the current object. 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 a 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 – learn java access modifiers

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 an example,

class Bank
{ 
   int m;
   int n;
 
   public void setValue(int m, int n)
   {
      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 a dot operator.

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;

Also read – string constructors in java

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 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 of by compiler and you do not need to append this keyword explicitly unless instance variable name and local variable name are same.

Related Posts