看了《深入Java虚拟机》一书中关于“类是否初始化?“ 这一问题的探讨后,觉得有必要亲手做下总结
在实际代码中, 有些类可能看上去时初始化了,但实际上并没有。
举个例子,
对于一个类Fruit, 声明一个fruit[] 数组,看上去是很多个Fruit对象被初始化了,其实一个都没有,下面就竟可能全面的讨论一下这个问题
检测方式
在类里加一个静态区域,如果类初始化,那么这段静态区域就会有输出
主动引用(会发生初始化)
1 遇到new、getstatic、putstatic、invokestatic这4条字节码指令时,如果类没有进行过初始化,则需要先触发其初始化。
生成这4条指令的几个最常见的Java代码场景是:
I. new 一个类的时候会发生初始化
- package test.class_life_cycle.initialize;
- class T{
- //when initialize the class T, then will come the output
- static{
- System.out.println("Initialize class T");
- }
- }
- public class Test0 {
- public static void main(String[] args) {
- T t = new T();
- }
- }
package test.class_life_cycle.initialize;
class T{
//when initialize the class T, then will come the output
static{
System.out.println("Initialize class T");
}
}
public class Test0 {
public static void main(String[] args) {
T t = new T();
}
}
输出:
II.调用类中的静态成员,除了final字段,看下面这个例子,final被调用但是没有初始化类
- package test.class_life_cycle.initialize;
- class A{
- static final int count = 1;
- //When call the class , this part is the first to execute
- static{
- System.out.println("Initialize class A");
- }
- }
- class Test1{
- public static void main(String[] args) {
- int x = A.count;
- }
- }
package test.class_life_cycle.initialize;
class A{
static final int count = 1;
//When call the class , this part is the first to execute
static{
System.out.println("Initialize class A");
}
}
class Test1{
public static void main(String[] args) {
int x = A.count;
}
}
但是没有任何输出,就是因为 那个final字段,Java编译器把这样的字段解析成对常量的本地拷贝(该常量存在于引用者类,这里就是Test1类 的常量池里或者字节码里)
III. 调用某个类中的静态方法,那个类一定先被初始化了
- package test.class_life_cycle.initialize;
- class X{
- static void Output(){
- System.out.println("Output !");
- }
- //When call the class , this part is the first to execute
- static{
- System.out.println("Initialize class X");
- }
- }
- public class Test1_3 {
- public static void main(String[] args) {
- X.Output();
- }
- }
package test.class_life_cycle.initialize;
class X{
static void Output(){
System.out.println("Output !");
}
//When call the class , this part is the first to execute
static{
System.out.println("Initialize class X");
}
}
public class Test1_3 {
public static void main(String[] args) {
X.Output();
}
}
输出:
2 使用java.lang.reflect包的方法对类进行反射调用的时候,如果类没有进行过初始化,则需要先触发其初始化。
通过调用java.lang.Class.forName(String className)
- package test.class_life_cycle.initialize;
- class B{
- static final int count = 1;
- //When call the class , this part is the first to execute
- static{
- System.out.println("Initialize class B");
- }
- }
- public class Test2 {
- public static void main(String[] args) throws
- ClassNotFoundException, InstantiationException, IllegalAccessException {
- Class classB = Class.forName("test.class_life_cycle.initialize.B");
- }
- }
package test.class_life_cycle.initialize;
class B{
static final int count = 1;
//When call the class , this part is the first to execute
static{
System.out.println("Initialize class B");
}
}
public class Test2 {
public static void main(String[] args) throws
ClassNotFoundException, InstantiationException, IllegalAccessException {
Class classB = Class.forName("test.class_life_cycle.initialize.B");
}
}
输出:
3 当初始化一个类的时候,如果发现其父类还没进行过初始化,则需要先触发其父类的初始化。
而且由下面的输出顺序可以看到先初始化父类,再是子类的
- package test.class_life_cycle.initialize;
- class Cfather{
- static{
- System.out.println("Initialize class Cfather");
- }
- }
- class Cson extends Cfather{
- static{
- System.out.println("Initialize class Cson");
- }
- }
- public class Test3 {
- public static void main(String[] args) {
- Cson son = new Cson();
- }
- }
package test.class_life_cycle.initialize;
class Cfather{
static{
System.out.println("Initialize class Cfather");
}
}
class Cson extends Cfather{
static{
System.out.println("Initialize class Cson");
}
}
public class Test3 {
public static void main(String[] args) {
Cson son = new Cson();
}
}
输出:
4 当虚拟机启动时,用户需要指定一个要执行的主类,虚拟机会先初始化这个主类。
其实就是public static void main(String[] args)所在的那个类
被动引用(看上去会,其实不会发生初始化):
1 通过子类引用父类的静态字段,不会导致子类初始化
下面这个例子中,虽然是以Dson.count 形式调用的,但是因为count是Dfather的静态成员变量,所以只初始化Dfather类,而不初始化Dson类
- package test.class_life_cycle.initialize;
- class Dfather{
- static int count = 1;
- static{
- System.out.println("Initialize class Dfather");
- }
- }
- class Dson extends Dfather{
- static{
- System.out.println("Initialize class Dson");
- }
- }
- public class Test4 {
- public static void main(String[] args) {
- int x = Dson.count;
- }
- }
package test.class_life_cycle.initialize;
class Dfather{
static int count = 1;
static{
System.out.println("Initialize class Dfather");
}
}
class Dson extends Dfather{
static{
System.out.println("Initialize class Dson");
}
}
public class Test4 {
public static void main(String[] args) {
int x = Dson.count;
}
}
2 通过数组定义类引用类,不会触发此类的初始化
- package test.class_life_cycle.initialize;
- class E{
- static{
- System.out.println("Initialize class E");
- }
- }
- public class Test5 {
- public static void main(String[] args) {
- E[] e = new E[10];
- }
- }
- <span style="color:#111111;"></span>
package test.class_life_cycle.initialize;
class E{
static{
System.out.println("Initialize class E");
}
}
public class Test5 {
public static void main(String[] args) {
E[] e = new E[10];
}
}
<span style="color:#111111;"></span>
但是没有输出
3 常量在编译阶段会存入调用类的常量池中,本质上没有直接引用到定义常量的类,因此不会触发定义常量的类的初始化
- package test.class_life_cycle.initialize;
- class F{
- static final int count = 1;
- static{
- System.out.println("Initialize class F");
- }
- }
- public class Test6 {
- public static void main(String[] args) {
- int x = F.count;
- }
- }
package test.class_life_cycle.initialize;
class F{
static final int count = 1;
static{
System.out.println("Initialize class F");
}
}
public class Test6 {
public static void main(String[] args) {
int x = F.count;
}
}
如上例中,F类中定义的count是final对象,其在编译阶段就会存入调用类的常量池中