案例1:
现象:父类的静态代码块没有执行
原因:
- 对于静态字段来说,只有直接定义了该字段的类才会被初始化;
- str是父类定义的静态字段
- 是否在编译期间MyChild1就被修改成了父类类型MyParent呢?这样的话就更容易理解了。
现象:父类的静态代码块被 自行了,子类的静态代码块也 执行了
原因:
子类初始化之前,一定会将父类先初始化完毕(对应初始化一个类的子类的情况)
案例2 接口变量作为编译期常量
//1.删除Parent08 和 Child08的class文件,程序正常。
public class Test08 {
public static void main(String[] args) {
System.out.println(ChileIf08 .str1);
}
}
interface Parent08{
static String str = "parent08";
}
interface ChileIf08 extends Parent08{
static String str2 = "ChileIf08";
//String str2 = "ChileIf08";
//static final String str2 = "ChileIf08";
//static String str2 = UUID.randomUUID().toString();
}
class Child08 implements Parent08{
static String str1 = "Child08";
}
查看输出:
C:\Users\rock\IdeaProjects\jvm-learn-07\interfaceclass\target\classes>javap -c Test08.class
Compiled from "Test08.java"
public class Test08 {
public Test08();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #4 // String ChileIf08
5: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}
测试结论:
对于接口来说:以下三个等同。都会作为编译期常量
//String str2 = "ChileIf08";
//static String str2 = "ChileIf08";
//static final String str2 = "ChileIf08";
案例3 接口变量作为运行期常量
public class Test08 {
public static void main(String[] args) {
System.out.println(ChileIf08.str2);
//System.out.println(Child08.str1);
}
}
interface Parent08{
static String str = "parent08";
}
interface ChileIf08 extends Parent08{
static String str2 = UUID.randomUUID().toString();
}
3.1删除Child和Parent的class文件运行结果为:
//Exception in thread "main" java.lang.NoClassDefFoundError: ChileIf08
3.2删除Parent的class文件,运行结果为:
Exception in thread "main" java.lang.NoClassDefFoundError: Parent08
3.3保留class文件,运行结果
//加载了父接口和子接口的class文件
[Loaded Parent08 from file:/C:/Users/rock/IdeaProjects/jvm-learn-07/_08interfaceclass/target/classes/]
[Loaded ChileIf08 from file:/C:/Users/rock/IdeaProjects/jvm-learn-07/_08interfaceclass/target/classes/]
3.4 证明接口初始化
//-XX:+TraceClassLoading
class MyPrinter
{
public MyPrinter(String owner){
System.out.println(owner + " : print..");
}
}
public class Test08 {
interface Child extends Parent {
String name = "child";
MyPrinter childTest = new MyPrinter("Child");
void init();
}
interface Parent {
String str ="strParent";
MyPrinter test = new MyPrinter("Parent");
}
public Test08() {
System.out.println("test is init ");
}
/* public static void main(String[] args) {
System.out.println(Child.name);
//输出child
System.out.println("-------------");
//输出-------------
//[Loaded Test08$Parent from file:/C:/Users/rock/IdeaProjects/jvm-learn-07/_08interfaceclass/target/classes/]
//[Loaded Test08$Child from file:/C:/Users/rock/IdeaProjects/jvm-learn-07/_08interfaceclass/target/classes/]
//[Loaded MyPrinter from file:/C:/Users/rock/IdeaProjects/jvm-learn-07/_08interfaceclass/target/classes/]
System.out.println(Child.childTest);//调用子接口的变量
//Child : print.. 说明子接口初始化了
//MyPrinter@4554617c
System.out.println("-------------");
//-------------
System.out.println(Child.childTest);//调用子接口的变量
//MyPrinter@4554617c 等同于第一个,也是运行期常量,只初始化一次
System.out.println("-------------");
//-------------
System.out.println(Child.test);//使用到了父接口的运行期常量
//Parent : print.. //执行了父接口的初始化
//MyPrinter@74a14482
System.out.println("-------------");
//-------------
System.out.println(Child.test);
//MyPrinter@74a14482//父接口只初始化了一次
}*/
public static void main(String[] args) {
System.out.println(Child.name);
//child
System.out.println("-------------");
//-------------
System.out.println(Child.str);//调用了父接口的编译期常量,并没有引起父接口和子接口的初始化,
System.out.println("-------------");
System.out.println(Child.test);//调用了父接口的运行期变量,并没有引起子接口的初始化
//[Loaded Test08$Parent from file:/C:/Users/rock/IdeaProjects/jvm-learn-07/_08interfaceclass/target/classes/]
//[Loaded Test08$Child from file:/C:/Users/rock/IdeaProjects/jvm-learn-07/_08interfaceclass/target/classes/]
//[Loaded MyPrinter from file:/C:/Users/rock/IdeaProjects/jvm-learn-07/_08interfaceclass/target/classes/]
//Parent : print..
//MyPrinter@4554617c
}
}
理论指导:
当一个接口在初始化时,并不要求其父接口都完成了初始化
只有在真正使用到父接口的时候(如引用接口中所定义的常量),才会初始化。
前三个案例,只通过删除文件验证了对class的加载过程,但加载和初始化不是一回事儿啊!啪啪脸疼!
案例4,在圣思园同学的帮助下,一个巧妙的方法来验证初始化。
类实现接口,实例化类
public class Test08 {
public static void main(String[] args) {
//System.out.println(ChileIf08.str2);
System.out.println(Child08.str1);
}
}
如果再删除Child和Parent的class文件,结果出现了变化
Exception in thread "main" java.lang.NoClassDefFoundError: Child08
at Test08.main(Test08.java:10)
Caused by: java.lang.ClassNotFoundException: Child08
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more