小编srn*_*jak的帖子

使用静态和最终限定符的奇怪Java行为

在我们的团队中,我们发现了一些奇怪的行为,我们使用了两者staticfinal限定词 这是我们的测试类:

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两次写入值,因为静态类成员的代码是从上到下执行的.

任何人都可以解释为什么会发生这种行为?

java final

76
推荐指数
4
解决办法
4002
查看次数

标签 统计

final ×1

java ×1