Java 中局部变量与成员变量同名时,局部变量会隐藏成员变量。如果我们想访问成员变量,可以使用 this 关键字。
class Test {
private int value = 10;
void method() {
int value = 40;
System.out.println("Value of Instance variable :" + this.value);
System.out.println("Value of Local variable :" + value);
}
}
public class UseTest {
public static void main(String args[]) {
Test obj1 = new Test();
obj1.method();
}
}
Value of Instance variable :10
Value of Local variable :40