关于Class对象的newInstance()方法
来源:2-1 Class类及对象实例化
易萧
2020-09-17 17:20:16
JDK 9之后,Class对象的newInstance()方法已经被弃用。
官方推荐通过Constructor对象来使用newInstance()。
* @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>
1回答
好帮手慕阿满
2020-09-17
同学你好,实际工作中大多数使用的是JDK8,所以课程是按照JDK8的版本来讲的。这里也感谢同学的反馈,我们会反馈给相关工作人员。
祝:学习愉快~
相似问题