递归初始化:访问 Class 字段时未调用静态初始化程序
Recursive initialization : Static Initializer not getting called when i access Class field
了解加载 class 和调用静态初始化程序
In what order do static initializer blocks
所以,我只是想确认一下 -
public class OOMErrorB extends OOMErrorA {
public static int c = 10;
static {
System.out.println("Loading static B " + c);
System.out.println(OOMErrorA.a);
}
public static void main(String[] args) {
new OOMErrorB();
}
}
父class是-
public class OOMErrorA {
public static int a = 20;
static {
a = a+ OOMErrorB.c;
System.out.println("OOMErrorB.c " + OOMErrorB.c);
System.out.println("loading OOMErrorA.a " + a);
}
}
现在 B 的主要方法的输出 -
**
OOMErrorB.c 0
loading OOMErrorA.a 20
Loading static B 10
20
**
我可以理解,首先它加载 class A 因为它是 Super class 并调用它的静态初始化程序,
现在因为我在 OOMErrorA 的静态块中访问 OOMErrorB.c,它应该加载并调用 OOMErrorB 的静态初始化器。
所以,OOMErrorB.c 应该是 10 而不是 0 。
我对 class -
的加载和初始化了解多少
1) Class and gets loaded and variables are initialized to default values like for int - 0, Object - null.
2) Class field are initialized to specified values.
3) Static block gets called .
在我的程序中,我可以看到 class OOMErrorB 已加载(第 1 步),但第 2 步和第 3 步没有执行。
而根据 link 上接受的答案,它应该调用 OOMErrorB 的静态初始化程序。
所以它应该以循环依赖结束?
当访问 OOMErrorB.c
时,OOMErrorB
未 加载,因为它已经在加载过程中(当 JVM 最初按顺序加载它时调用 main
方法)。 JVM一旦加载了一个class,就不会再加载了。因此,不会出现循环依赖:OOMErrorB
的静态成员c
被fetch了,此时还没有初始化
你可以检查this section from the Java language specification关于class初始化:
Because the Java programming language is multithreaded, initialization of a class or interface requires careful synchronization, since some other thread may be trying to initialize the same class or interface at the same time. There is also the possibility that initialization of a class or interface may be requested recursively as part of the initialization of that class or interface; for example, a variable initializer in class A might invoke a method of an unrelated class B, which might in turn invoke a method of class A. The implementation of the Java Virtual Machine is responsible for taking care of synchronization and recursive initialization by using the following procedure.
JVM 有其正确的方法来锁定 classes 的初始化,从而防止递归初始化。
了解加载 class 和调用静态初始化程序
In what order do static initializer blocks
所以,我只是想确认一下 -
public class OOMErrorB extends OOMErrorA {
public static int c = 10;
static {
System.out.println("Loading static B " + c);
System.out.println(OOMErrorA.a);
}
public static void main(String[] args) {
new OOMErrorB();
}
}
父class是-
public class OOMErrorA {
public static int a = 20;
static {
a = a+ OOMErrorB.c;
System.out.println("OOMErrorB.c " + OOMErrorB.c);
System.out.println("loading OOMErrorA.a " + a);
}
}
现在 B 的主要方法的输出 -
**
OOMErrorB.c 0
loading OOMErrorA.a 20
Loading static B 10
20
**
我可以理解,首先它加载 class A 因为它是 Super class 并调用它的静态初始化程序,
现在因为我在 OOMErrorA 的静态块中访问 OOMErrorB.c,它应该加载并调用 OOMErrorB 的静态初始化器。 所以,OOMErrorB.c 应该是 10 而不是 0 。
我对 class -
的加载和初始化了解多少1) Class and gets loaded and variables are initialized to default values like for int - 0, Object - null.
2) Class field are initialized to specified values.
3) Static block gets called .
在我的程序中,我可以看到 class OOMErrorB 已加载(第 1 步),但第 2 步和第 3 步没有执行。
而根据 link 上接受的答案,它应该调用 OOMErrorB 的静态初始化程序。
所以它应该以循环依赖结束?
当访问 OOMErrorB.c
时,OOMErrorB
未 加载,因为它已经在加载过程中(当 JVM 最初按顺序加载它时调用 main
方法)。 JVM一旦加载了一个class,就不会再加载了。因此,不会出现循环依赖:OOMErrorB
的静态成员c
被fetch了,此时还没有初始化
你可以检查this section from the Java language specification关于class初始化:
Because the Java programming language is multithreaded, initialization of a class or interface requires careful synchronization, since some other thread may be trying to initialize the same class or interface at the same time. There is also the possibility that initialization of a class or interface may be requested recursively as part of the initialization of that class or interface; for example, a variable initializer in class A might invoke a method of an unrelated class B, which might in turn invoke a method of class A. The implementation of the Java Virtual Machine is responsible for taking care of synchronization and recursive initialization by using the following procedure.
JVM 有其正确的方法来锁定 classes 的初始化,从而防止递归初始化。