static块和构造函数的执行顺序

来源:互联网 发布:mac pro 显卡驱动升级 编辑:程序博客网 时间:2024/05/17 04:55

package com.test;

public class OrderTest {
    public static void main(String[] args) {
        /*
        * 结果:
            
parent static block
            Child static block
            parent construct block
            Child construct block
        */

        new Child();
        /*
        *
        *
parent construct block
           Child construct block
        */

        new Child();
    }
}

class Parent {
    static {
        System.out.println("parent static block");
    }

    public Parent() {
        System.out.println("parent construct block");
    }
}

class Child extends Parent {
    static {
        System.out.println("Child static block");
    }

    public Child() {
        System.out.println("Child construct block");
    }
}

执行顺序
1 父类的静态代码块
2 子类的静态代码块
3 父类的构造方法
4 子类的构造方法

原创粉丝点击