Java中的实例变量隐藏

具有相同名称的局部变量将隐藏实例变量。为了使用实例变量,我们应该使用this运算符。请参阅下面的示例-

示例

public class Tester{
   int a = 1;

   public static void main(String[] args) {
      Tester t = new Tester();
      t.show();
      t.show1();
   }

   public void show(){
      int a = 2;
      System.out.println(a);
   }

   public void show1(){
      int a = 3;
      System.out.println(this.a);
   }
}

输出结果

2
1