考虑一下:
public class TestClass {
private String a;
private String b;
public TestClass()
{
a = "initialized";
}
public void doSomething()
{
String c;
a.notify(); // This is fine
b.notify(); // This is fine - but will end in an exception
c.notify(); // "Local variable c may not have been initialised"
}
}
我不明白。“b”从未初始化,但会给出与“c”相同的运行时错误,这是一个编译时错误。为什么局部变量和成员之间有区别?
编辑:将成员设为私有是我的初衷,但问题仍然存在......