Java9 Class类 newInstance 过时
Class.forName("类的全限定名").newInstance();
1
被替换为
Class.forName("类的全限定名").getDeclaredConstructor().newInstance();
1
源码说明
/**
* Creates a new instance of the class represented by this {@code Class}
* object. The class is instantiated as if by a {@code new}
* expression with an empty argument list. The class is initialized if it
* has not already been initialized.
*
* @deprecated This method propagates any exception thrown by the
* nullary constructor, including a checked exception. Use of
* this method effectively bypasses the compile-time exception
* checking that would otherwise be performed by the compiler.
* The {@link
* java.lang.reflect.Constructor#newInstance(java.lang.Object...)
* Constructor.newInstance} method avoids this problem by wrapping
* any exception thrown by the constructor in a (checked) {@link
* java.lang.reflect.InvocationTargetException}.
*
* <p>The call
*
* <pre>{@code
* clazz.newInstance()
* }</pre>
*
* can be replaced by
*
* <pre>{@code
* clazz.getDeclaredConstructor().newInstance()
* }</pre>
*/
感谢优快云博主「吃口雪花」
————————————————
版权声明:本文为优快云博主「吃口雪花」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/qq_39424178/article/details/98121862