最终字段初始化顺序
Final fields initialization order
这里有一些代码在尚未初始化的 class 上调用静态方法 A.f() 。
有人可以用 JLS 解释这段代码的行为吗?
class A {
final static Object b = new B();
final static int S1 = 1;
final static Integer S2 = 2;
static void f() {
System.out.println(S1);
System.out.println(S2);
}
}
class B {
static {
A.f();
}
}
public class App
{
public static void main( String[] args )
{
A.f();
}
}
输出:
1
null
1
2
A.f()
in App.main()
触发 class A
.
的初始化
所有常量变量都已初始化。唯一的常数变量是 S1
,现在是 1
.
然后,其他静态字段按文本顺序初始化。 b
是第一个字段,它触发 class B
的初始化,进而调用 A.f()
。 S2
只是 null
因为它还没有初始化。 b
的初始化现已完成。最后但同样重要的是,S2
被初始化为 Integer
对象 2
.
S2
不是常量变量,因为它不是原始类型int
而是引用类型Integer
。 S2 = 2;
是 S2 = Integer.valueOf(2);
的自动装箱 shorthand。
If a declarator in a field declaration has a variable initializer, then the declarator has the semantics of an assignment (§15.26) to the declared variable.
[…]
Note that static
fields that are constant variables (§4.12.4) are initialized before other static
fields (§12.4.2). This also applies in interfaces (§9.3.1). Such fields will never be observed to have their default initial values (§4.12.5), even by devious programs.
A constant variable is a final
variable of primitive type or type String
that is initialized with a constant expression (§15.28). Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9), and definite assignment (§16 (Definite Assignment)).
A constant expression is an expression denoting a value of primitive type or a String
that does not complete abruptly and is composed using only the following:
- Literals of primitive type and literals of type
String
[…]
For each class or interface C, there is a unique initialization lock LC
. The mapping from C to LC
is left to the discretion of the Java Virtual Machine implementation. The procedure for initializing C is then as follows:
[…]
Otherwise, record the fact that initialization of the Class
object for C is in progress by the current thread, and release LC
.
Then, initialize the static
fields of C which are constant variables (§4.12.4, §8.3.2, §9.3.1).
[…]
- 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.
12.4.2. Detailed Initialization Procedure
Every variable in a program must have a value before its value is used:
Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10.2):
[…]
- For all reference types (§4.3), the default value is
null
.
看来这个问题不属于JLS
,但是我们已经处理了JVMS
。
在 resolution
流程之前的 Linking
阶段有 Preparation 子阶段,其中涉及:
creating the static fields for a class or interface and initializing
such fields to their default values
以及更多:
explicit initializers for static fields are executed as part of
initialization (§5.5), not preparation
而 initialization 包括:
The execution of any one of the Java Virtual Machine instructions new
基本类型的初始化包括写入它们的初始值。对于引用类型字段,它们的默认值是 null
因为在 Resolution substage jvm 没有 "know" 之前 class 与 class 的适当符号引用名称相关联.
这里有一些代码在尚未初始化的 class 上调用静态方法 A.f() 。 有人可以用 JLS 解释这段代码的行为吗?
class A {
final static Object b = new B();
final static int S1 = 1;
final static Integer S2 = 2;
static void f() {
System.out.println(S1);
System.out.println(S2);
}
}
class B {
static {
A.f();
}
}
public class App
{
public static void main( String[] args )
{
A.f();
}
}
输出:
1
null
1
2
A.f()
in App.main()
触发 class A
.
所有常量变量都已初始化。唯一的常数变量是 S1
,现在是 1
.
然后,其他静态字段按文本顺序初始化。 b
是第一个字段,它触发 class B
的初始化,进而调用 A.f()
。 S2
只是 null
因为它还没有初始化。 b
的初始化现已完成。最后但同样重要的是,S2
被初始化为 Integer
对象 2
.
S2
不是常量变量,因为它不是原始类型int
而是引用类型Integer
。 S2 = 2;
是 S2 = Integer.valueOf(2);
的自动装箱 shorthand。
If a declarator in a field declaration has a variable initializer, then the declarator has the semantics of an assignment (§15.26) to the declared variable.
[…]
Note that
static
fields that are constant variables (§4.12.4) are initialized before otherstatic
fields (§12.4.2). This also applies in interfaces (§9.3.1). Such fields will never be observed to have their default initial values (§4.12.5), even by devious programs.
A constant variable is a
final
variable of primitive type or typeString
that is initialized with a constant expression (§15.28). Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9), and definite assignment (§16 (Definite Assignment)).
A constant expression is an expression denoting a value of primitive type or a
String
that does not complete abruptly and is composed using only the following:
- Literals of primitive type and literals of type
String
[…]
For each class or interface C, there is a unique initialization lock
LC
. The mapping from C toLC
is left to the discretion of the Java Virtual Machine implementation. The procedure for initializing C is then as follows:[…]
Otherwise, record the fact that initialization of the
Class
object for C is in progress by the current thread, and releaseLC
.Then, initialize the
static
fields of C which are constant variables (§4.12.4, §8.3.2, §9.3.1).[…]
- 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.
12.4.2. Detailed Initialization Procedure
Every variable in a program must have a value before its value is used:
Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10.2):
[…]
- For all reference types (§4.3), the default value is
null
.
看来这个问题不属于JLS
,但是我们已经处理了JVMS
。
在 resolution
流程之前的 Linking
阶段有 Preparation 子阶段,其中涉及:
creating the static fields for a class or interface and initializing such fields to their default values
以及更多:
explicit initializers for static fields are executed as part of initialization (§5.5), not preparation
而 initialization 包括:
The execution of any one of the Java Virtual Machine instructions new
基本类型的初始化包括写入它们的初始值。对于引用类型字段,它们的默认值是 null
因为在 Resolution substage jvm 没有 "know" 之前 class 与 class 的适当符号引用名称相关联.