我有以下两个类(在两个单独的文件中)。
public class Foo
{
public static class A
{
public static final boolean FLAG = false;
}
public final A A = new A();
}
public class Bar
{
void method()
{
if (Foo.A.FLAG) <<<< this is giving "Cannot make static ref to non-static field
// do something
;
}
}
我的问题是,为什么编译器不能识别Foo.A
,我指的是类A
,而不是成员,它恰好也被命名为A
发布于 2013-12-17 06:11:27
访问标志的一些方法:
<Foo_A extends Foo.A> void test1()
{
if(Foo_A.FLAG)
;
}
void test2()
{
class Foo_A extends Foo.A{}
if(Foo_A.FLAG)
;
}
-------------------------------------
import pkg.Foo.A;
public class Bar
{
void test3()
{
if(A.FLAG)
;
}
}
在这些上下文中,"Foo.A"
只能解释为类型,而不能解释为变量。
发布于 2013-12-17 05:36:10
因为内部类需要封闭类型的实例。如果你没有Foo的实例,A就不存在。
编辑-这是不正确的,但原因是信息性的。请看下面的评论:
https://stackoverflow.com/questions/20621371
复制相似问题