Practice mode Java

What must be true of a local variable from the enclosing method that is used inside a Java lambda body?

Correct answer B. It must be final or effectively final

Explanation

A lambda may capture a local variable only if that variable is final or effectively final, meaning it is never reassigned after initialisation. The lambda may outlive the method, so it works with a copy of the value, and reassignment would make that copy misleading. Fields do not have this restriction, as they are accessed through objects.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.