A lambda expression is created and invoked inside method foo() of class Inner, which is an inner class of class Outer:
// import java.util.function.*;
class Outer {
int a = 1; // Outer field a
class Inner {
int b = 2; // Inner field b
void foo() { // Inner method foo
int c = 3; // Local variable c
// create lambda
Consumer<Integer> r = (x) -> {
/* Modify x, c, b, a */
x++; c++; b++; a++; // ERROR!!
};
// invoke lambda
r.accept(4);
} // foo end
} // Inner end
} // Outer end
The lambda expression refers to 3 variables from its outer scope: local variable c of foo, field b of Inner, and field a of Outer. The lambda expression itself takes an integer parameter x.
Above code fails to compile because one of x, c, b, and a cannot be modified inside the lambda. Which one of the 4 variables you think cannot be modified inside the lambda expression?