JVM 主动类和被动类的使用
主动使用和被动使用Demo 1、创建工程一个Gradle工程 下一步 下一步 点击完成 2、创建类 public class MyTest1 { public static void main(String[] args) { System.out.println(MyChild1.str); } } class MyParent1{ public static String str = "hello world"; static { System.out.println("MyParent1 static block"); } } class MyChild1 extends MyParent1{ static { System.out.println("MyChild static block"); } } 输出结果: MyParent1 static block hello world 会发现MyChild1的类静态块没有执行。 总结:对于静态字段来说,只有直接定义了该字段的类才会被初始化。 修改后的类: public class MyTest1 { public static void main(String[] args) { System.out.println(MyChild1.str2); } } class MyParent1{ public