this keyword in java
Local Variable Hides Instance Variable In java when the local variable has the same name as an instance variable, the local variable hides the instance variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Test { int a=10, b=10;// instance variable a,b public static void main(String agrs[]) { Test t = new Test(); t.display(); } public void display() { int a = 5;//local variable same name as instance variable System.out.println("Value of local variable a=" + a); System.out.println("Value of instance variable a=" + a + " b=" + b); } } |
Result
1 2 3 | Value of local variable a=5 Value of instance variable a=5 b=10 |
In above program we have two instance variable with value a=10 and b=10. in method display there is a local variable a with value 5. Inside … Read more