zoukankan      html  css  js  c++  java
  • forName 和.class

    package com.example.demo.reflact;
    
    public class A {
        public static String cc = "cccc";
        static {
            cc="32323232";
            System.out.println("静态初始化2322323");
        }
    
    
        public void hello() {
            System.out.println("hello world"+cc);
        }
    }

    区别:

    (1)Class.forName除了将类的.class文件加载到jvm中之外,还会对类进行解释,执行类中的static块。

    (2)而classloader只干一件事情,就是将.class文件加载到jvm中,不会执行static中的内容,只有在newInstance才会去执行static块。

    #Class.forName(name,initialize,loader)带参数也可控制是否加载static块。并且只有调用了newInstance()方法采用调用构造函数,创建类的对象。


    ClassLoader就是遵循双亲委派模型最终调用启动类加载器的类加载器,实现的功能是“通过一个类的全限定名来获取描述此类的二进制字节流”,获取到二进制流后放到JVM中。Class.forName()方法实际上也是调用的CLassLoader来实现的。



    作者:代码之尖
    链接:https://www.jianshu.com/p/2dbfec55c987
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    package com.example.demo.reflact;
    
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class TestClassLoad {
    
        public static void main(String[] args) throws Exception {
    //        m1();
    //        m2();
            m3();
    //        m4();
            return;
    
        }
    
        private static void m4() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
            A a = new A();
            Class<?> clz = a.getClass();
            Object o = clz.newInstance();
            Method m = clz.getDeclaredMethod("hello", null);
            m.invoke(o);
    
        }
    
        private static void m3() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
            Class<?> clz = A.class;
            Object o = clz.newInstance();
            Method m = clz.getDeclaredMethod("hello", null);
            m.invoke(o);
    
        }
    
        private static void m2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
            Class<?> clz = Class.forName("com.example.demo.reflact.A");
            Object o = clz.newInstance();
            Method m = clz.getDeclaredMethod("hello", null);
            m.invoke(o);
        }
    
        private static void m1() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
            Class<?> clz = Class.forName(A.class.getName());
            Object o = clz.newInstance();
            Method m = clz.getDeclaredMethod("hello", null);
            m.invoke(o);
        }
    
    }

    参考:https://www.cnblogs.com/Marydon20170307/p/9815325.html

  • 相关阅读:
    应用图标大小
    AndroidStudio使用笔记
    shell 三剑客之 sed 命令详解
    shell 三剑客之 sed pattern 详解
    shell 文本处理三剑客之 grep 和 egrep
    Shell 编程中的常用工具
    shell 函数的高级用法
    shell 数学运算
    shell 变量的高级用法
    nginx 之 https 证书配置
  • 原文地址:https://www.cnblogs.com/leeego-123/p/13994083.html
Copyright © 2011-2022 走看看