I\'m playing with lambdas in Java 8 and I came across warning local variables referenced from a lambda expression must be final or effectively final
. I know tha
public class LambdaScopeTest {
public int x = 0;
class FirstLevel {
public int x = 1;
void methodInFirstLevel(int x) {
// The following statement causes the compiler to generate
// the error "local variables referenced from a lambda expression
// must be final or effectively final" in statement A:
//
// x = 99;
}
}
}
As others have said, a variable or parameter whose value is never changed after it is initialized is effectively final. In the above code, if you change the value of x
in inner class FirstLevel
then the compiler will give you the error message:
Local variables referenced from a lambda expression must be final or effectively final.