Before java 8 an inner class could access outer objects only if they were declared final. However now when I run example code (from below) on javaSE 1.8 there is no compilation error and program runs fine.
Why did they change that and how does It work now?
Example code from java 7 tutorial:
public class MOuter {
private int m = (int) (Math.random() * 100);
public static void main(String[] args) {
MOuter that = new MOuter();
that.go((int) (Math.random() * 100), (int) (Math.random() * 100));
}
public void go(int x, final int y){
int a = x + y;
final int b = x - y;
class MInner{
public void method(){
System.out.println("m is "+m);
System.out.println("x is "+x); // supposedly illegal - 'x' not final
System.out.println("y is: "+y);
System.out.println("a is "+a); // supposedly illegal? - 'a' not final
}
}
MInner that = new MInner();
that.method();
}
}