Java Try-With-Resources: Effectively Final Variables in JDK 9+
Given: Which statement is true? - 
Community Votes
100% of anonymous learners picked answer A. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
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.
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.
Community Discussion (3 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
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. Sinceres1 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 asfinal; 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?
In JDK 9+, variables only need to be effectively final (not reassigned) rather than explicitly declared with the final keyword to be used in try-with-resources.
What changed in JDK 9 regarding try-with-resources?
JDK 9 extended the scope of resources to include effectively final variables defined outside the try statement, whereas JDK 7/8 required declaration inside the parentheses.