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.


This also applies for anonymous inner classes

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)