GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Class.newInstance() to constructor reflection | java.evolved

Home / Tooling / Class.newInstance() to constructor reflection
Tooling Intermediate

Class.newInstance() to constructor reflection

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

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

Tooling Migration Constructors Exception Handling
Code Comparison
✕ Deprecated Class.newInstance()
Plugin plugin = pluginClass.newInstance();
✓ Constructor reflection
Plugin plugin = pluginClass
        .getDeclaredConstructor()
        .newInstance();
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
✅

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
JDK Support
Class.newInstance() to constructor reflection
Available

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

How it works

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.

Related Documentation
Class.getDeclaredConstructor() ↗ Constructor.newInstance() ↗
Proof
View proof source ↗
Related patterns
Tooling Advanced

Class-file parsing with the standard API

Third-party bytecode parser
ClassReader reader = new ClassReader(
        Files.readAllBytes(classFile));
reader.accept(visitor, 0);
Java 24+
ClassModel model = ClassFile.of().parse(classFile);
model.methods().forEach(method ->
        System.out.println(
                method.methodName().stringValue()));
Hover to see modern ➜
JDK 24+ →
I/O Intermediate

Deprecated URL constructors to URI

Deprecated URL constructor
URL endpoint =
    new URL("https://example.com/api?q=java");
Java 20+
URI endpointUri =
    URI.create("https://example.com/api?q=java");
URL endpoint = endpointUri.toURL();
Hover to see modern ➜
JDK 20+ →
Language Intermediate

Compact canonical constructor

Java 16
public record Person(String name,
                     List<String> pets) {
    // Full canonical constructor
    public Person(String name,
                  List<String> pets) {
        Objects.requireNonNull(name);
        this.name = name;
        this.pets = List.copyOf(pets);
    }
}
Java 16+
public record Person(String name,
                     List<String> pets) {
    // Compact constructor
    public Person {
        Objects.requireNonNull(name);
        pets = List.copyOf(pets);
    }
}
Hover to see modern ➜
JDK 16+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.