jvm的学习笔记:二、类的初始化,代码实战(4)

  • 当接口被初始化的时候,不要求其父类被初始化
    • System.out.println(MyChild5.c);
    • 输出:
    • MyChild5
    • 1
    • 依据:new Random().nextInt(3)并非编译区间可以确定的,所以初始化了MyChild5,但是没有初始化MyParent5。

public class MyTest5 {

    public static void main(String[] args) {
        System.out.println(MyChild5.c);
    }

}

interface MyParent5{

    /*接口里的成员都是默认 pulic static final*/
    public static final int a = 5;

    Thread thrad = new Thread(){
        {
            System.out.println("MyParent5");
        }
    };
}

interface MyChild5 extends MyParent5{

    Thread thrad = new Thread(){
        {
            System.out.println("MyChild5");
        }
    };

    int b = 6;

    int c = new Random().nextInt(3);
}

猜你喜欢

转载自www.cnblogs.com/boychen/p/11674854.html