GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev — Class.newInstance() to constructor reflection | java.evolved
Tooling
Intermediate
Class.newInstance() to constructor reflectionReplace deprecated Class.newInstance() with explicit constructor lookup and invocation. Code Comparison
✕ Deprecated Class.newInstance()
Plugin plugin = pluginClass.newInstance();
✓ Constructor reflection
Plugin plugin = pluginClass
.getDeclaredConstructor()
.newInstance();
See a problem with this code? Let us know.
Why the modern way wins
✅
Supported APIRemoves use of deprecated Class.newInstance(). 🎯
Explicit constructorThe requested signature is visible and can accept arguments. 🛡
Honest failuresConstructor 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
Proof
Related patterns
Tooling
Advanced
Class-file parsing with the standard APIThird-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()));
JDK 24+
→
I/O
Intermediate
Deprecated URL constructors to URIDeprecated 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();
JDK 20+
→
Language
Intermediate
Compact canonical constructorJava 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);
}
}
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. |