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? 1 2 Answers to Quote Java...
Class.newInstance() is marked deprecated. Documentation does not suggest any alternatives. How are we meant to create instances now?
2 Answers
To quote Java 9's javadoc:
The call
clazz.newInstance()can be replaced by
clazz.getDeclaredConstructor().newInstance()
Class.getDeclaredConstructor(...).newInstance(...)
Refer to Google errorprone's documentation (for example) for a description of why.