Learn This Keyword In Java

Sharing is healthy !!

Hello everyone!! We are going to learn this keyword in java.

Learn This Keyword In Java

Let’s learn above concept with an 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 This_Example {

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 a method setAccount() to set the values for ‘m’ and ‘n’. To display the values for ‘m’ and ‘n’ i have created a method showAccount(). A main method wherein we create an object of bank class and call methods setAccount() and showAccount(). When you run the above program you will get the below output,

Learn This Keyword In Java

Our expected output ‘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 the 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. The problem can be solved by ‘this‘ keyword. See example below,

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 This_Example {

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 your object calls method setAccount() the keyword ‘this’ is replaced by the object’s handler ‘bk’. Now the compiler knows the ‘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 the compiler implicitly appends the instance variables with the ‘this’ keyword. Such that when ‘bk1’ is calling a setAccount() method instance variables are appended by its reference variable. While ‘bk2’ is calling the setAccount() method instance variables of ‘bk2’ are modified. This process is taken care of by compiler itself and you do not need to append this keyword explicitly unless there is some exceptional situation as in our earlier example.

Sharing is healthy !!

Leave a Reply

Your email address will not be published.