Learn this keyword in java

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

For example,

class Bank
{ 
   int m;
   int n;
 
   public void setAccount(int m,int n)
   {
      m = m;
      n = n;
   }
 
   public void showAccount()
   {
      System.out.println("Bank Account M = " + m);
      System.out.println("Bank Account N = " + n);
   }
}

public class ThisExample
{
   public static void main(String[] args)
   {
      Bank bk = new Bank();
      bk.setAccount(1, 4);
      bk.showAccount();
   }
}

In the above example I have created a class Bank with instance variables “m” and “n” and method setAccount() to set the values for “m” and “n”.

To display the values for “m” and “n” we have created method showAccount(). A main method wherein we create an object of class Bank and call methods setAccount() and showAccount().

When you run the above program you will get below output,

Learn This Keyword In Java

Our expected output for “m” and “n” should be initialized to the values 1 and 4 respectively. But the value is zero!!!??

Also read – learn operators in java

Let’s analyse,

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

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

Hence it does not set the value of “m” when method setAccount is called. This problem can be solved by ‘this’ keyword. Here’s an example,

class Bank
{ 
   int m;
   int n;
 
   public void setAccount(int m,int n)
   {
      this.m = m;
      this.n = n;
   }
 
   public void showAccount()
   {
      System.out.println("Bank Account M = " + m);
      System.out.println("Bank Account N = " + n);
   }
}

public class ThisExample
{
   public static void main(String[] args)
   {
      Bank bk = new Bank();
      bk.setAccount(1, 4);
      bk.showAccount();
   }
}

In setAccount() method we should append both “m” and “n” with the “this” keyword followed by a dot operator.

During code execution when object calls method setAccount() the keyword “this” is replaced by the object’s handler ‘bk’.

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.

Learn This Keyword In Java

Suppose, say, a user choose different names for instance variables and method arguments.

In this case we must create two objects of the class each calling the setAccount() method.

public static void main(String[] args)
{ 
   Bank bk1 = new Bank();
   bk1.setAccount(1, 4);
   Bank bk2 = new Bank();
   bk2.setAccount(2,3);
}

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

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

Such that when “bk1” calls setAccount() method instance variables are appended by its reference variable. Similarly for “bk2”.

This process is taken care of by compiler and you do not need to append this keyword explicitly unless there is some exceptional situation as in our earlier example.

Related Posts