16

可能重复:
Java 静态类初始化

为什么在初始化块中更新字符串变量而不是整数(即使先写入块)

class NewClass
{
    static 
    {
       System.out.println(NewClass.string+" "+NewClass.integer);
    }

    final static String string="static";
    final static Integer integer=1;

    public static void main(String [] args)//throws Exception
    {
    }
}

我的输出是

static null

PS:还注意到只有当我插入 final 修饰符时,字符串变量初始化才会发生在块之前。为什么会这样?为什么不使用整数?我也将其声明为最终静态

4

3 回答 3

20

从JLS 的第 12.4.2 节,适当地剪断:

初始化 C 的过程如下:

  • 然后,初始化最终类变量和接口字段,其值为编译时常量表达式(§8.3.2.1、§9.3.1、§13.4.9、§15.28)。

  • 接下来,按照文本顺序执行类的类变量初始化程序和静态初始化程序,或者接口的字段初始化程序,就好像它们是一个单独的块一样。

因此,对于非编译时常量,这不是“所有变量”然后是“所有静态初始化程序”的情况,反之亦然——它们都是按文本顺序排列在一起的。所以如果你有:

static int x = method("x");

static {
    System.out.println("init 1");
}

static int y = method("y");

static {
    System.out.println("init 2");
}

static int method(String name) {
    System.out.println(name);
    return 0;
}

那么输出将是:

x
init 1
y
init 2

即使 makexyfinal 在这里也不会影响这一点,因为它们仍然不是编译时常量。

PS:还注意到只有当我插入 final 修饰符时,字符串变量初始化才会发生在块之前。

那时,它是一个编译时常量,它的任何使用基本上都是内联的。此外,变量值在其余初始化程序之前分配,如上所述。

JLS 的第 15.28 节定义了编译时常量——它包括所有原始值和String,但包括包装类型,例如Integer.

于 2012-09-16T16:11:34.767 回答
7

这是对您问题的简短直接回答....

static Variable

静态变量在JVM 加载时执行Class,而在实例或调用Class 时加载。static method

static Block or static Initializer Block

static static Initializer BlockClass实例化之前或static method被调用之前被初始化,甚至static variable被使用之前。

//////// 编辑部分 /////////

class NewClass {

    final static String string = "static";
    final static Integer integer = 1;

    static {
        System.out.println(NewClas.string + " " + NewClas.integer);
    }

    public static void main(String [] args) { // throws Exception
        new NewClas();
    }

}

以上将打印 static 1

原因是JVM它将执行称为的优化过程Constant folding,对常量变量进行预计算。

此外,在您的情况下,结果static null原因Constant folding应用于 Primitive 类型而不是 Wrapper Object,在您的情况下,它的 Integer ...

于 2012-09-16T16:24:34.350 回答
1

它们按给定的顺序(字段和静态块)初始化,这就是为什么打印值为null,没有分配给在静态块之后定义的静态字段。

于 2012-09-16T16:09:51.067 回答