编码时,我遇到了一个奇怪的Java编译器行为.
编译类(下面的源代码)时,编译器inner classes cannot have static declarations会对NULL类变量发出错误(" ").这是预期的!
但是,ZERO类变量不会生成错误.这个我不明白!
为什么这种差异似乎允许内部类中的简单类型而不是对象的静态声明.
(javac -version:1.6.0_24)
public class Outer {
public static final Runnable HELLO = new Runnable() {
// No compiler error
public static final int ZERO = 0;
// Causes compiler error: "inner classes cannot have static declarations"
public static final Object NULL = null;
@Override
public void run() {
System.out.println("Hello " + ZERO + NULL);
}
};
}
Run Code Online (Sandbox Code Playgroud)