Tooling Intermediate

Class.newInstance() to constructor reflection

Replace deprecated Class.newInstance() with explicit constructor lookup and invocation.

✕ Deprecated Class.newInstance()
Plugin plugin = pluginClass.newInstance();
✓ Constructor reflection
Plugin plugin = pluginClass
        .getDeclaredConstructor()
        .newInstance();
See a problem with this code? Let us know.

Supported API

Removes use of deprecated Class.newInstance().

🎯

Explicit constructor

The requested signature is visible and can accept arguments.

🛡

Honest failures

Constructor exceptions are represented through InvocationTargetException.

Old Approach
Class.newInstance()
Modern Approach
Explicit constructor lookup
Since JDK
9
Difficulty
Intermediate
Class.newInstance() to constructor reflection
Available

Recommended since Class.newInstance() was deprecated in JDK 9 (September 2017).

Class.newInstance() is deprecated because it can bypass compile-time checking for exceptions thrown by the constructor and only supports a zero-argument constructor. Explicit Constructor lookup makes the selected signature visible and reports reflective invocation failures through the appropriate reflection exceptions. Module and access rules still apply; this pattern does not recommend bypassing encapsulation.