Class.newInstance()
is marked deprecated. Documentation does not suggest any alternatives. How are we meant to create instances now?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
To quote Java 9's javadoc:
The call
clazz.newInstance()
can be replaced by
clazz.getDeclaredConstructor().newInstance()
回答2:
Class.getDeclaredConstructor(...).newInstance(...)
Refer to Google errorprone's documentation (for example) for a description of why.