zoukankan      html  css  js  c++  java
  • JVM 初始化阶段例子

    创建如下Demo

    package com.example.jvm.classloader;
    
    
    class Parent{
        static  int a = 3;
    
        static {
            System.out.println("Parent static block");
        }
    }
    
    class  Child extends  Parent{
        static  int b = 4;
    
        static {
            System.out.println("Child static block");
        }
    }
    
    public class MyTest9 {
        static {
            System.out.println("MyTest9  static block");
        }
    
        public static void main(String[] args) {
    
            System.out.println(Child.b);
        }
    }
    

      运行结果

    MyTest9  static block
    Parent static block
    Child static block
    4
    

      

    创建Demo2

    package com.example.jvm.classloader;
    
    
    
    class Parent2{
        static  int a  = 3;
    
        static {
            System.out.println("Parent2 static block");
        }
    }
    
    class  Child2 extends  Parent2{
    
        static  int b  = 4;
    
        static {
            System.out.println("Child2 static block");
        }
    
    }
    public class MyTest10 {
        static {
            System.out.println("MyTest10 static block");  //静态块被打印,说明该类被初始化了。
        }
    
        public static void main(String[] args) {
    
            Parent2 parent2;
    
            System.out.println("______________");
    
            parent2 = new Parent2();
    
            System.out.println(parent2.a);
    
            System.out.println("______________");
    
            System.out.println(Child2.b);
    
        }
    
    }
    

      打印结果:

    MyTest10 static block
    ______________
    Parent2 static block
    3
    ______________
    Child2 static block
    4
    

      

    创建Demo3

    class Parent3{
        static  int a  = 3;
    
        static {
            System.out.println("Parent3 static block");
        }
    
        static void doSomething(){
            System.out.println("do something");
        }
    }
    
    class  Child3 extends  Parent3{
    
        static  int b  = 4;
    
        static {
            System.out.println("Child3 static block");
        }
    
    }
    public class MyTest11 {
    
        public static void main(String[] args) {
    
            //变量定义在哪里,就是对哪个类的主动使用。如a定义在Parent3中,那就是对Parent3的主动使用
            System.out.println(Child3.a);
            System.out.println("-----------");
    
            //静态方法定义在哪里,就是对哪个类的主动使用。如doSomething定义在Parent3中,那就是对Parent3的主动使用
            Child3.doSomething();
    
        }
    
    }
    

      打印结果

    Parent3 static block
    3
    -----------
    do something
    

      

    4、Demo4 使用反射使类初始化

    class CL{
    
    
        static {
            System.out.println("CL static block");
        }
    
    
    }
    public class MyTest12 {
    
        public static void main(String[] args)  throws  Exception{
            ClassLoader loader = ClassLoader.getSystemClassLoader();
            Class<?> clazz =  loader.loadClass("com.example.jvm.classloader.CL");
    
            System.out.println(clazz);
    
            System.out.println("-------------");
    
            clazz = Class.forName("com.example.jvm.classloader.CL");
    
            System.out.println(clazz);
        }
    }
    

      打印结果

    class com.example.jvm.classloader.CL
    -------------
    CL static block
    class com.example.jvm.classloader.CL
    

      说明反射Class.forName会使类初始化。而调用ClassLoader类的loadClass方法加载一个类,并不是对类的主动使用,不会导致类的初始化

  • 相关阅读:
    将迁移学习用于文本分类 《 Universal Language Model Fine-tuning for Text Classification》
    深度 | 提升深度学习模型的表现,你需要这20个技巧(附论文)
    [线性代数] 矩阵白化
    基于搜索的贝叶斯网络结构学习算法-K2
    Deep learning:四十三(用Hessian Free方法训练Deep Network)
    2020年AI、CV、NLP顶会最全时间表
    浅谈人脸识别中的loss 损失函数
    控制uniFrame显示的一个管理类
    php+sqlserver之如何操作sqlserver数据库
    php支持连接sqlserver数据库
  • 原文地址:https://www.cnblogs.com/linlf03/p/10994652.html
Copyright © 2011-2022 走看看