Effectively Final Variable Usage in Java Lambda Expressions

1Z0-819 By islever Editorial Last updated

Short answer

C The program prints "Java" because the local variable is effectively final.

Question

Given: What is the result? - Exhibit: Effectively Final Variable Usage in Java Lambda Expressions
  1. Oracle
  2. An exception is thrown at run time.
  3. Java Correct Answer
  4. Compilation error

Key Takeaways

The exam tests the 'effectively final' constraint for lambda captures; the trap is assuming any reference variable assignment causes a compilation error, ignoring that immutability allows access.

This question tests the rule that local variables accessed by lambda expressions must be effectively final. The code compiles and runs correctly because the variable is not modified after initialization.

Common mistake: Candidates often choose Compilation Error (D) because they confuse the requirement for explicit `final` with the broader concept of 'effectively final', failing to notice the variable isn't reassigned.

Expert Analysis

Why the Answer Is Correct

The correct answer is C (Java). In Java, a local variable used inside a lambda expression does not need to be explicitly declared as final, provided it is 'effectively final'. A variable is effectively final if its value is initialized once and never changed thereafter. Since the string s is assigned "Oracle" and never reassigned, the compiler accepts the lambda -> System.out.println(s).

Why the Other Options Are Wrong

Option A (Oracle) is incorrect because the lambda prints the value of s at execution time, which is "Java", not "Oracle". Option B is incorrect because no runtime exception occurs; the code is valid. Option D (Compilation Error) is the most common distractor; it is wrong because the restriction only applies to variables whose values change, or to parameters that are reassigned within the lambda body.

Community Comment Notes

Community consensus strongly supports the compilation success. One user noted that "Local variables... can be accessed in a lambda expression provided and they are effectively final," confirming the logic. Another commenter emphasized that "If s is modified then code not compile," highlighting the conditional nature of the rule.

Exam Strategy

Always check if a captured variable is reassigned anywhere in its scope. If it is not, it is effectively final and legal in lambdas. Do not assume all local variables require an explicit final keyword.

Frequently Asked Questions

What is the difference between final and effectively final?

Final requires explicit declaration; effectively final means the compiler infers it because the value is never reassigned.

Can I modify the array object referenced by an effectively final variable?

Yes. Effectively final restricts reassignment of the reference, not modification of the object's internal state.

More 1Z0-819 FAQ →

Community Votes

D
100%

100% of anonymous learners picked answer D. 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

  1. ASPushkin 👍 2
    right answer is D Local variables decalred in the enclosing method, including its formal parameters, can be accessed in a lambda expression provided and they are effectively final This one : String s = "Oracle"; Runnable r = () -> { System.out.println(s); }; //s = "Java"; Thread t = new Thread(r); t.start() will be ok
  2. d7bb0b2 👍 1 Answered D
    variables in lambdas must be a final or effectively final. If s is modified then code not compile
  3. Omnisumem 👍 2 Answered D
    tested

Reader comments & corrections

No reader corrections 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.

← Back to 1Z0-819 Study Guide