Try-with-resources improvement
Use existing effectively-final variables directly in try-with-resources.
Code Comparison
✕ Java 8
Connection conn = getConnection();
// Must re-declare in try
try (Connection c = conn) {
use(c);
}
✓ Java 9+
Connection conn = getConnection();
// Use existing variable directly
try (conn) {
use(conn);
}
Why the modern way wins
No re-declaration
Use the existing variable name directly.
Less confusion
No separate variable name inside the try block.
Concise
Fewer lines, same resource safety.
Old Approach
Re-declare Variable
Modern Approach
Effectively Final
Since JDK
9
Difficulty
beginner
JDK Support
Try-with-resources improvement
Available
Widely available since JDK 9 (Sept 2017)
How it works
Java 9 allows effectively-final variables to be used directly in try-with-resources without re-declaration. This is cleaner when the resource was created outside the try block.