以什么顺序执行的类中的静态块和静态变量? [英] in what order are static blocks and static variables in a class executed?
问题描述
可能重复:
Java静态类初始化
为什么字符串变量在初始化块而不是整数(即使先写入块)
class NewClass
{
static
{
System.out.println(NewClass.string ++ NewClass.integer);
}
final static String string =static;
final static整数整数= 1;
public static void main(String [] args)//抛出异常
{
}
}
我的输出是
static null
PS:还注意到只有当我插入final修饰符时,字符串变量初始化才会发生在块之前。为什么会这样?为什么不对整数呢?我已经宣布它为最终静态
来自第12.4.2节,适当地剪断:
初始化C的过程如下:
然后,初始化其值为编译时常量表达式的接口的最终类变量和字段(§8.3.2.1,§9.3.1,§13.4.9,§15.28)。 / p>
接下来,按文本顺序执行类的类变量初始值设定项和静态初始值设定项,或接口的字段初始值设定项,就像它们是单个一样阻止。
因此,对于非编译时常量,它不是所有变量的情况,然后是所有静态初始化器,反之亦然 - 它们全部在一起,在文字顺序。所以如果你有:
static int x = method(x);
static {
System.out.println(init 1);
}
static int y = method(y);
static {
System.out.println(init 2);
}
static int方法(字符串名称){
System.out.println(name);
返回0;
}
然后输出为:
x
init 1
y
init 2
即使制作 x
或 y
final也不会影响到此,因为它们仍然不是编译时常量。
PS:还注意到字符串变量初始化发生在块之前,只有当我插入最终修饰符。
此时,它是一个编译时常量,并且它的任何使用基本上都是内联的。此外,变量值在其余的初始值设定项之前分配,如上所述。
第15.28节定义了编译时常量 - 它包括所有原始值和字符串
,但不包装类型,例如 Integer
。
Possible Duplicate:
Java static class initialization
Why is the string variable updated in the initialization block and not the integer(even though the block is written first)
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
{
}
}
My output is
static null
P.S:Also noticed that string variable initialization happens before the block only when i insert the final modifier. why is that?why not for integer as well?I have declared it as final static too
From section 12.4.2 of the JLS, snipped appropriately:
The procedure for initializing C is then as follows:
Then, initialize the final class variables and fields of interfaces whose values are compile-time constant expressions (§8.3.2.1, §9.3.1, §13.4.9, §15.28).
Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.
So for non-compile-time-constants, it's not a case of "all variables" and then "all static initializers" or vice versa - it's all of them together, in textual order. So if you had:
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;
}
Then the output would be:
x
init 1
y
init 2
Even making x
or y
final wouldn't affect this here, as they still wouldn't be compile-time constants.
P.S:Also noticed that string variable initialization happens before the block only when i insert the final modifier.
At that point, it's a compile-time constant, and any uses of it basically inlined. Additionally, the variable value is assigned before the rest of the initializers, as above.
Section 15.28 of the JLS defines compile-time constants - it includes all primitive values and String
, but not the wrapper types such as Integer
.
这篇关于以什么顺序执行的类中的静态块和静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!