What to use instead of Class.newInstance()?

Class.newInstance() is marked deprecated. Documentation does not suggest any alternatives. How are we meant to create instances now?

84800 次浏览
Class.getDeclaredConstructor(...).newInstance(...)

有关原因的描述,请参考 谷歌错误倾向文档(例如)。

引用 Java9的 javadoc的话:

电话

clazz.newInstance()

可以被

clazz.getDeclaredConstructor().newInstance()