why we are using this in java

Solutions on MaxInterview for why we are using this in java by the best coders in the world

showing results for - "why we are using this in java"
Marie
27 Feb 2019
1/*this keyword in Java is a reference variable that refers to the current object 
2of a method or a constructor. The main purpose of using this keyword in Java is
3to remove the confusion between class attributes and parameters that have same names.
4For Example; if we have */
5
6class Account{
7int a;
8int b;
9
10 public void setData(int a ,int b){
11  a = a;
12  b = b;
13 }
14 public void showData(){
15   System.out.println("Value of A ="+a);
16   System.out.println("Value of B ="+b);
17 }
18 public static void main(String args[]){
19   Account obj = new Account();
20   obj.setData(2,3);
21   obj.showData();
22 }
23}
24/*it's Output will be a=0 & b=0;
25but if we replace this.a=a & this.b=b;
26So now the answer will be a=2 ,a=3.*/