zoukankan      html  css  js  c++  java
  • 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 static String str = "hello world";
    
        static {
            System.out.println("MyParent1 static block");
        }
    }
    
    class MyChild1 extends  MyParent1{
    
        public static String str2 = "hello world 2";
    
        static {
            System.out.println("MyChild static block");
        }
    }
    

      执行结果

    MyParent1 static block
    MyChild static block
    hello world 2
    

     因为使用到了MyChild的类,它会被初始化。当一个类在初始化时,要求其父类全部都已经初始化完毕。最终打印结果如上面所示。 

    3、上面1中,MyChild1没有被实例化,那MyChild类是否有被加载?

    -XX:+TraceClassLoading, 用于追踪类的加载信息并打印出来
    配置如下:

    完整的代码

    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{
    
        public static String str2 = "hello world 2";
    
        static {
            System.out.println("MyChild static block");
        }
    }
    

      

    打印的结果

    说明MyChild类也会被加载, 最先加载的是MyTest1类

    JVM参数

    -XX:+<option>, 表示开启option选项

    -XX:+<option> 表示关闭options选项
    -XX:<option>=<value>, 表示将option选项的值设置为value

     4、首次主动使用例子

    public class MyTest4 {
    
        public static void main(String[] args) {
            MyParent4 myParent4 = new MyParent4();
            System.out.println("-------------");
            MyParent4 myParent5 = new MyParent4();
        }
    }
    
    class MyParent4{
    
        static {
            System.out.println("MyParent4 static block");
        }
    

      打印结果:

    MyParent4 static block
    -------------
    

      说明在创建MyParent4 对象的时候,会初始化MyParent4, 但是第二次创建MyParent4的时候,就不会初始化MyParent4。所有只在主动首次使用才会初始化。

    
    
  • 相关阅读:
    结对项目刘畅2016012040
    Daily target小队介绍(刘畅,陈杰,杨有存,唐祎琳,王晓哲,邵汝佳)
    第四、十七章读书报告
    2016012040+小学四则运算练习软件项目报告
    JAVA之路(二)
    使用pip命令可能遇到的报错
    monkey命令
    Mitmproxy 手机配置代理
    appium安装与简单使用
    自动化测试第十一节---unittest
  • 原文地址:https://www.cnblogs.com/linlf03/p/10989141.html
Copyright © 2011-2022 走看看