内/外部类加载顺序问题

2019-10-24  本文已影响0人  _micang

public class AA {

private static int _var =1;

{

        System.out.println("外部类构造_1_" + System.currentTimeMillis());

}

AA() {

        System.out.println("外部类构造_2_" + System.currentTimeMillis());

}

static {

        System.out.println("外部类静态块加载" + System.currentTimeMillis());

}

public static class BB{

    private static int _bb_static_1 =12;

    static {

        System.out.println("内部类静态加载" + System.currentTimeMillis());

    }

    BB() {

        System.out.println("内部类构造" + System.currentTimeMillis());

    }

}

    public static void main(String[] args) {

        System.out.println(BB._bb_static_1);

        System.out.println(BB._bb_static_1);

        new AA();

    }

}

执行结果: 

外部类静态块加载1571897780400

内部类静态加载1571897780405

12

12

外部类构造_1_1571897780406

外部类构造_2_1571897780406

上一篇下一篇

猜你喜欢

热点阅读