编程语言
首页 > 编程语言> > Java反射 - getDeclaredConstructor().newInstance()得到实例化对象

Java反射 - getDeclaredConstructor().newInstance()得到实例化对象

作者:互联网

Java反射 - getDeclaredConstructor().newInstance()得到实例化对象

实现类

public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
    UserService.class.getDeclaredConstructor().newInstance();
    System.out.println("=====================");
    UserService.class.newInstance();
}

class对象代码

public class UserService {

    static {
        System.out.println("static");
    }
    
    public UserService() {
        System.out.println("test");
    }
}

得到的结果

image-20211129102651601

标签:Java,newInstance,System,实例,getDeclaredConstructor,UserService,class
来源: https://www.cnblogs.com/HeiDaotu/p/15618894.html