简化顺序版本是:
- 父类的静态成员赋值和静态块,代码排版先后顺序执行,只运行一次
- 子类的静态成员赋值和静态块,代码排版先后顺序执行,只运行一次
- 父类代码块
- 父类构造方法
- 子类初始化块
- 子类构造方法
再次构造子类,将重复3/4/5/6步骤
总之:见到静态必先行-》类中代码块-》构造。
public class Hello {
static class A{
static C c =new C();
public A(){
System.out.println("A's Constructor");
}
static {
System.out.println("A's static ");
}
}
static class B extends A{
public B(){
System.out.println("B's Constructor");
}
D d = new D();
C c =new C();
static {
System.out.println("B's static ");
}
}
static class C {
public C(){
System.out.println("C's Constructor");
}
static {
System.out.println("C's static");
}
}
static class D{
public D(){
System.out.println("D's Constructor");
}
static {
System.out.println("D's static");
}
}
public static void main(String args[]){
System.out.println("start");
B b = new B();
}
}
结果:
start
C's static
C's Constructor
A's static
B's static
A's Constructor
D's static
D's Constructor
C's Constructor
B's Constructor
第二例子:
http://blog.csdn.net/wenwen091100304/article/details/49488137