Variable used in lambda expression should be final or effectively final

Although other answers prove the requirement, they don’t explain why the requirement exists.

The JLS mentions why in ยง15.27.2:

The restriction to effectively final variables prohibits access to dynamically-changing local variables, whose capture would likely introduce concurrency problems.

To lower risk of bugs, they decided to ensure captured variables are never mutated.

Leave a Comment