https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#accessing-local-variables

 

 

Like local and anonymous classes, lambda expressions can capture variables.

A non final local variable or method parameter whose value is never changed after initialization is known as effectively final.

 

Here x is effectively final:

int x=99;

Consumer<Integer> myConsumer = (y) -> System.out.println("x = " + x);

myConsumer.accept(x);

 

 

The following statement causes the compiler to generate

the error "local variables referenced from a lambda expression

must be final or effectively final.

 

 

Here x is no more effectively final:

int x=99;

//The following statement causes the compiler to generate
//the error "local variables referenced from a lambda expression
//must be final or effectively final".

x=x+1;

Consumer<Integer> myConsumer = (y) -> System.out.println("x = " + x);

myConsumer.accept(x);