下产生一个错误:静态嵌套类的(实例)可以访问外部静态变量吗?
class A {
static int apple = 5;
static class StaticNestedSubClassOfA {
//...
}
}
class Test {
public static void main(String str[]) {
A.StaticNestedSubClassOfA b = new A.StaticNestedSubClassOfA();
System.out.println("Apple: " + b.apple);
}
}
ERROR:
Test.java:14: error: cannot find symbol System.out.println("Apple: " + b.apple); ^ symbol: variable apple location: variable b of type StaticNestedSubClassOfA 1 error
但不StaticNestedSubClassOfA
访问静态变量apple
? (我想这并不意味着从StaticNestedSubClassOfA
催生对象访问apple
通过他们的情况?)
为什么不把它称为'A.apple',而不考虑上下文? – rgettman
'StaticNestedSubClassOfA'可以访问'A.apple',但这不是你想要做的。您正尝试从类型为'StaticNestedSubClassOfA'的参考表达式中访问名为'apple'的字段。 –