1.加载基类,初始化基类中的静态成员变量。
2.加载派生类,初始化派生类中的静态成员变量。
3.运行派生类的main方法。
4.初始化基类中的其他成员变量(static类型的除外)。
5.调用基类构造函数。
6.初始化派生类中的其他成员变量(static类型的除外)。
7.调用派生类构造函数。
for example:
Beetle.java
class Insect {
private int i = 9;
private int l = print("Insect.l initialized");
protected int j ;
public Insect(){
System.out.println("i="+i+" j="+j);
j = 39;
}
private static int x1 = print("static Insect.x1 initialized");
static int print(String s){
System.out.println(s);
return 47;
}
}
public class Beetle extends Insect{
private int k = print("Beetle.k initialized");
//private int j=100;
public Beetle(){
System.out.println("k = "+k);
System.out.println("j = "+j);
}
private static int y = print("Beetle.y initialized");
public static void main(String[] args) {
System.out.println("Beetle constructor");
Beetle b = new Beetle();
//Insect i = new Insect();
}
}
运行输出:
static Insect.x1 initialized
Beetle.y initialized
Beetle constructor
Insect.l initialized
i=9 j=0
Beetle.k initialized
k = 47
j = 39