码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA9之后废弃newInstance()方法

时间:2019-07-17 19:06:21      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:abc   lang   targe   declared   void   chm   lock   tde   tac   

JAVA9之后废弃newInstance()方法

根据JAVA11的API 我们可以看见反射中的newInstance()方法不推荐使用了,用

clazz.getDeclaredConstructor().newInstance()

代替

例如

package reflect;
 
import java.lang.reflect.InvocationTargetException;
 
/**
 * 获取class对象的几种方法
 * 对象.get.class
 * 类 class
 * Class.forName(全限定类名)
 *
 */
public class Demo {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
        String string="abc";
        Class<?> class1=string.getClass();
        class1=String.class;
        try {
            class1=Class.forName("java.lang.String");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println((String)class1.getDeclaredConstructor().newInstance());
    }
 
}

JAVA9之后废弃newInstance()方法

标签:abc   lang   targe   declared   void   chm   lock   tde   tac   

原文地址:https://www.cnblogs.com/wenqiangit/p/11202664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!