静态变量初始化顺序

时间:2022-10-11 19:42:53

1. 代码

public class A {

public static int num01 = B.num02;

public static A instance01_a  = new A("A的对象"); // 打印输出二:执行构造函数 A

public static final int finalInt =  (int) (Math.random()*100);

public static B instance02_b = new B("B的对象"); // 打印输出三:执行构造函数 B

 

public static final String finalStr = "finalStr";

public static final Integer finalInteger = new Integer(10);

public static int num02 = 1;

public static C instance03_c = null; 

 

public A(String from){

System.out.println("------------begin A::A------------");

System.out.println("A::A, from = "+from);

System.out.println("A::A, A.num01 = "+A.num01);

System.out.println("A::A, B.num02 = "+B.num02);

System.out.println("A::A, B.instance_a = "+B.instance_a);

System.out.println("------------end A::A------------");

}

public static void main(String[] args) {

System.out.println("main, A.num01 = "+ A.num01);

System.out.println("main, B.num01 = "+B.num01);

System.out.println("main, C.instance = "+C.instance_a); // 打印输出四:构造函数 A

}

 

}

class B{

public static int num01 = A.num02;

public static A instance_a = new A("A的对象");  // 打印输出一:执行构造函数 A

public static int num02 = 1;

 

public B(String from){

System.out.println("------------start B::B------------");

System.out.println("B::B, from = "+from);

System.out.println("B::B, B.num02 = "+num02);

System.out.println("B::B, A.num02 = "+A.num02);

System.out.println("B::B, A.instance02_b = "+A.instance02_b);

System.out.println("B::B, A.instance01_a = " +A.instance01_a);

System.out.println("B::B, A.finalInt = "+A.finalInt);

System.out.println("B::B, A.finalStr = "+A.finalStr);

System.out.println("B::B, A.finalInteger = "+A.finalInteger);

System.out.println("------------end B::B------------");

 

}

}

 

class C{

public static final A instance_a = new A("A的对象");

}

 

2. 打印结果:

------------begin A::A------------

A::A, from = A的对象

A::A, A.num01 = 0

A::A, B.num02 = 0

A::A, B.instance_a = null

------------end A::A------------

------------begin A::A------------

A::A, from = A的对象

A::A, A.num01 = 1

A::A, B.num02 = 1

A::A, B.instance_a = cn.zzz.day03.A@7852e922

------------end A::A------------

------------start B::B------------

B::B, from = B的对象

B::B, B.num02 = 1

B::B, A.num02 = 0

B::B, A.instance02_b = null

B::B, A.instance01_a = cn.zzz.day03.A@70dea4e

B::B, A.finalInt = 97

B::B, A.finalStr = finalStr

B::B, A.finalInteger = null

------------end B::B------------

main, A.num01 = 1

main, B.num01 = 0

------------begin A::A------------

A::A, from = A的对象

A::A, A.num01 = 1

A::A, B.num02 = 1

A::A, B.instance_a = cn.zzz.day03.A@7852e922

------------end A::A------------

main, C.instance = cn.zzz.day03.A@5c647e05

 

3.初始化过程分析:(只分析打印的第一个构造函数中)

(1)只有主动请求一个类时,该类才会初始化。初始化仅包括静态变量、函数等静态部分

(2)继承关系时先初始化父类,再初始化子类

(3)静态变量会按照声明顺序,依次声明并设置该类型的默认值,(此时不赋值为初始化的值)

(4)声明完毕后,再按照声明顺序依次设置为初始化的值,如果没有初始化则跳过

(5)当初始化到A.num01 = B.num02时,暂停初始化A.num01,设置当前类为B,跳转到步骤3并执行

(6)当初始化到 B.instance_a = new A("A的对象")时,暂停初始化B.instance_a, 实例化A并赋值给B.instance_a

(7)(接上)实例化A的过程中,需要B.num02, B.num02的当前值为默认值0。 【B.instance_a的值,但是后者还未初始化并处于暂停状态】; B.instance_a也是如此,当前值为null。