- 按照声明顺序初始化父类中的静态变量
- 按照声明顺序初始化子类中的静态变量
- 将为对象分配的内存设为二进制零值,因此对象的所有基本类型成员都会被设为默认值,对象引用成员会被设为null
- 按照声明顺序初始化父类中的实例变量
- 父类的构造器被调用
- 按照声明顺序初始化子类中的实例变量
- 子类的构造器被调用
补充:代码块紧跟着成员变量的后面被初始化
示例代码,来源于Thinking in Java 4th
//: reusing/Beetle.java
// The full process of initialization.
import static net.mindview.util.Print.*;
class Insect {
private int i = 9;
protected int j;
Insect() {
print("i = " + i + ", j = " + j);
j = 39;
}
private static int x1 =
printInit("static Insect.x1 initialized");
static int printInit(String s) {
print(s);
return 47;
}
}
public class Beetle extends Insect {
private int k = printInit("Beetle.k initialized");
public Beetle() {
print("k = " + k);
print("j = " + j);
}
private static int x2 =
printInit("static Beetle.x2 initialized");
public static void main(String[] args) {
print("Beetle constructor");
Beetle b = new Beetle();
}
} /* Output:
static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
i = 9, j = 0
Beetle.k initialized
k = 47
j = 39
*///:~