Variable used in lambda expression should be final or effectively final
时间: 2023-10-27 14:48:48 浏览: 95
In Java, a variable used in a lambda expression should be final or effectively final. This means that the variable should not be modified after it is initialized.
Effectively final means that the variable is not explicitly declared as final, but it is effectively final because its value is not changed after initialization.
This restriction is imposed because lambda expressions capture the values of variables at the time they are created, rather than when they are executed. If the value of a captured variable changes after the lambda expression is created, it can lead to unexpected behavior.
To avoid this issue, it is recommended to use final or effectively final variables in lambda expressions.