子父类间 静态代码块,代码块,构造函数的运行顺序

class HelloA {
    public HelloA() {
        System.out.println("HelloA");
    }
    {
        System.out.println("I'm A class");
    }
    static {
        System.out.println("static A");
    }
}
class HelloB extends HelloA {
    public HelloB() {
        System.out.println("HelloB");
    }
    {
        System.out.println("I'm B class");
    }
    static {
        System.out.println("static B");
    }
}
public class Demo {
    public static void main(String[] args) {
        System.out.println("main start");
        new HelloB();
        new HelloB();
        System.out.println("main end");
    }
}

 结果

main start
static A
static B
I'm A class
HelloA
I'm B class
HelloB
I'm A class
HelloA
I'm B class
HelloB
main end

 

posted @ 2016-07-31 13:38  damon2016  阅读(226)  评论(0编辑  收藏  举报