Java Try-With-Resources: Effectively Final Variables in JDK 9+

Exception handling
Answer Correct 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.

Given: Which statement is true? - image

  1. The code compiles successfully. Correct Answer
  2. The code fails to compile as MyResource must implement Closeable.
  3. The code fails to compile as try-with-resource needs a variable declaration such as MyResource r1 = res1; MyResource r2 = res2;.
  4. The code fails to compile as res2 should be declared as final.

Community Votes

A
100%

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)

cathDev 👍 1 Selected: A
Tested A
d7bb0b2 👍 1
A is correct cause, Class implement autoclosable and can variables declarated before try catch
ASPushkin 👍 2
right 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

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

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?

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.

More 1Z0-819 FAQ →

Related Analysis

← Back to 1Z0-819 Study Guide