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

相关文章:

  • 2021-09-13
  • 2021-09-08
  • 2021-07-26
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2021-11-01
猜你喜欢
  • 2022-01-09
  • 2021-06-03
  • 2021-09-12
  • 2021-11-21
  • 2021-11-17
  • 2022-01-07
相关资源
相似解决方案