While reading today, I saw a local inner class, which says that local internal classes can access local variables, but must be final. Because a local variable disappears after a method call, the local variable is stored in the heap with the final declaration, with the same declaration period as the inner class. But I wrote a local inner class that could access a non-final local variable, what's the matter? PS: My JDK is 8 does it have anything to do with this?
public class Jubuneibulei {
public void P (int a, int b) {
Class te{
void print () {
System.out.println (a);
}
}
New Te (). print ();
}
public static void Main (string[] args) {
New Jubuneibulei (). P (111, 222);
}
}
Not required in JDK8, but JDK will default to final
JDK7 the use of local variables in anonymous inner classes is not required in addition to FINAL,JDK8, but the JDK will default to final