Java Try-With-Resources: Effectively Final Variables in JDK 9+
Short answer
A The code compiles successfully because JDK 9+ allows effectively final variables declared outside the try block to be used in try-with-resources.
Question

Key Takeaways
The exam tests whether you know that JDK 9 expanded try-with-resources syntax to accept effectively final variables, removing the need for declaration inside the parentheses.
This question tests the Java try-with-resources feature introduced in JDK 7 and enhanced in JDK 9. The code compiles successfully because JDK 9 allows resources to be effectively final variables declared outside the try block.
Common mistake: Many candidates choose C or D, incorrectly applying JDK 7 rules where variables had to be declared within the try statement and were implicitly final.
Expert Analysis
Why the Answer Is Correct
The correct answer is A. The code uses a class implementing AutoCloseable (inferred from context of such questions) and utilizes it in a try-with-resources statement. In JDK 9 and later, the language specification was updated to allow resources to be effectively final variables that are declared before the try statement. Since res1 and res2 are not reassigned after initialization, they are effectively final, making the code valid and compilable.
Why the Other Options Are Wrong
Option B is incorrect because the class likely implements AutoCloseable, which is sufficient for try-with-resources; explicit implementation of Closeable is not strictly required if AutoCloseable is implemented. Option C reflects the pre-JDK 9 restriction where variables had to be declared inside the try parentheses. Option D is incorrect because the variables do not need to be explicitly declared as final; they just need to be effectively final (i.e., not reassigned).
Community Comment Notes
Community consensus strongly supports option A. Users note that this is a JDK 9 feature, with one comment stating "JDK9 One one = new One; try(one)" highlighting the syntax change. Another user confirms that "Class implement autoclosable and can variables declarated before try catch," validating the compilation success.
Exam Strategy
When seeing try-with-resources questions, always check the Java version context implied by the options. If the code shows variables declared outside the try block, assume JDK 9+ semantics unless specified otherwise. Remember that 'effectively final' is the key constraint.
Frequently Asked Questions
Why is the variable not required to be declared final?
What changed in JDK 9 regarding try-with-resources?
Community Votes
100% of anonymous learners picked answer A. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 3 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 3
-
ASPushkin 👍 2right answer is A //JDK 7 One one = new One(); // final One one = new One(); try (One temp = one; .....;) { //JDK9 One one = new One(); try(one) { and "one" is an effectively final
-
d7bb0b2 👍 1A is correct cause, Class implement autoclosable and can variables declarated before try catch
-
cathDev 👍 1 Answered ATested A
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.