package com.atzhangwl.jvm.classloader;
/**
* @ClassName Run_01
* @Description 演示java程序主动使用和被动使用 。类加载的顺序:Run_01 => MyParent1 => MyChild1
* @Author zhangwl
* @Date 2019/12/29 22:36
* @Version 1.0
**/
@SuppressWarnings("all")
public class Run_01 {
public static void main(String[] args) {
//演示1:说明对于静态字段来说,只有直接定义了该字段的类才会被初始化。主动访问
//-XX:+TraceClassLoading :用于追踪类的加载信息并且打印出来
//在这种情况,并没有对MyChild1进行初始化,但是对其进行了加载
System.out.println(MyChild1.parentStr);
//演示2
// System.out.println(MyChild1.childStr);
}
}
@SuppressWarnings("all")
class MyParent1 {
public static String parentStr = "parentStr";
static {
System.out.println("this static block is from parent");
}
}
@SuppressWarnings("all")
class MyChild1 extends MyParent1 {
public static String childStr = "childStr";
static {
System.out.println("this static block is from child");
}
}