Reputation: 435
class MyOuter2{
private String x;
void doStuff(){
int mloc= 100;
x ="Outer class variable";
class MyInner{
public void seeOuter(){
System.out.println("Access both: " + x+" and " + mloc);
System.out.println("Inner class ref is " + this);
System.out.println("Outer class ref is " + MyOuter2.this);
}
}
MyInner MethodInnerClass = new MyInner();
MethodInnerClass.seeOuter();
}
}
public class InnerClass {
public static void main(String[] args) {
MyOuter2 outer = new MyOuter2();
outer.doStuff();
}
}
In this code I use doStuff()'s variable mloc in method local MyInner Class my compiler compile this code and also run this code through JDK8.
Upvotes: 1
Views: 121
Reputation: 11
In Java SE 8, a local class can access local variables and parameters of the enclosing block that are final or effectively final. A variable or parameter whose value is never changed after it is initialized is effectively final.
Upvotes: 1
Reputation: 69
Method-local inner class variable can access in Java 8, but not Java 7. I use java version "1.8.0_31", and it can compile. But it cannot compile in Java 7.
Upvotes: 0