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 is used to refer instance variables. For example,

class Bank
{ 
   int m;
   int n;
 
   public Bank(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(1, 4);
      bk.showAccount();
   }
}

In the above example I have created a class Bank with instance variables “m” and “n” and constructor Bank() to set 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 showAccount() method.

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 java access modifiers

Let’s analyse,

In the constructor Bank() the arguments are declared with names “m” and “n”. While the 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 constructor Bank() is called. This problem can be solved by “this” keyword. Here’s an example,

class Bank
{ 
   int m;
   int n;
 
   public Bank(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(1, 4);
      bk.showAccount();
   }
}

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

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

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.

Learn This Keyword In Java

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

In this case we must create two objects of class Bank each calling constructor Bank().

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

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 constructor Bank() 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 instance variable name and local variable name are same .

Related Posts