JVM 接口初始化规则

1、创建两个接口,MyParent5接口,MyChild5 接口继承自MyParent5接口

public class MyTest5 {

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

interface MyParent5{

    public static final int a = 5;

}

interface MyChild5 extends MyParent5{
    public static final int b = 6;

}

  接口成员变量默认为:public static final

 

 打印结果

6

 

1、删除MyParent5.class 程序还是能正常运行。

 2、接着讲MyChild5.class 程序还是能正常运行  

总结: 当一个接口在初始化时,并不要求其父类接口都完成了初始化。 

只有在真正使用父接口的时候(如引用接口中所定义的常量时),才会初始化。




原文地址:https://www.cnblogs.com/linlf03/p/10990416.html