在我们的团队中,我们发现了一些奇怪的行为,我们使用了两者static和final限定词 这是我们的测试类:
public class Test {
public static final Test me = new Test();
public static final Integer I = 4;
public static final String S = "abc";
public Test() {
System.out.println(I);
System.out.println(S);
}
public static Test getInstance() { return me; }
public static void main(String[] args) {
Test.getInstance();
}
}
Run Code Online (Sandbox Code Playgroud)
当我们运行该main方法时,我们得到一个结果:
null
abc
Run Code Online (Sandbox Code Playgroud)
我会理解它是否null两次写入值,因为静态类成员的代码是从上到下执行的.
任何人都可以解释为什么会发生这种行为?