In a .java
file, following will not compile:
class Test {
public static void main(String[] args) {
int x = 0;
Runnable r = () -> System.out.println(x);
r.run();
x++;
r.run();
}
}
However in jshell, this will work and the output of the first r.run()
is 0
and for the second, it is 1
. So I want to know how is x
accessible to r
?