java static 代码块, 构造函数, 普通代码块执行顺序

简介

没有答出来, 根据如下代码的运行结果可知, 显示父类的静态代码块, 然后是子类的静态代码块, 然后是父类的普通代码块和构造函数, 接着是子类的普通代码块和构造函数.

code

/**
 * Created by lee on 2021/8/24.
 */
public class Father {
    static {
        System.out.println(("Father F0"));
    }
    {
        System.out.println("Father F1");
    }
    public Father() {
        System.out.println("Father F2");
    }

    public static void main(String[] args) {
        Son s = new Son(22);
    }
}

class Son extends Father{
    static {
        System.out.println("Son S0");
    }
    {
        System.out.println("Son S1");
    }
    public Son(Integer a) {
        super();
        System.out.println("Son S2");
    }
}

answer

Father F0
Son S0
Father F1
Father F2
Son S1
Son S2
Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
原文地址:https://www.cnblogs.com/eat-too-much/p/15178935.html