Before java 8, the inner class could access external objects only if they were declared final. However, now when I run the sample code (below) on javaSE 1.8, there are no compilation errors and the program works fine.
Why did they change this and how does it work now?
Sample 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);
Marco source share