/** 4, the inner class of the method can not access the local variables in the method, why? * * Yes, but must be final decorated to access. * * Reason: * First, when the method is called to run, the local variable is extinct. But the inner class object may still exist, * it will not die until it is referenced. At this point, there is a situation where the inner class accesses a non-existent local variable. * * Two, the solution to this problem is to use the final modified local variables, by the final local variable "copy" a copy, * Copy directly as a data member in the method inner class, this method internal class access is actually a copy of this local variable! * Moreover, since the final modified variable can no longer be modified, it ensures that the copy is consistent with the original variable. * * Three, the reason why two functions can be realized is: Java has a copy of the local variable (copy local variables) to achieve, * that is defined as the final copy of the local variables to use, and the reference can also be used, but not to re- Assigned value. * This creates the illusion that access local variable (access to locals) is possible, and this time because it can't be re-assigned, * so it doesn't usually cause unpredictable things to happen. * * Four, using the final modifier will not only keep the object's references from changing, * But the compiler will also continue to maintain the object's life cycle in the callback method. * So this is the fundamental meaning of the final variable and final parameter. * * and jdk1.8 added the effectively final feature, * Local internal classes and anonymous internal classes access the locals can not add the final modifier, by default added by the system. * */classTest4 { Public Static voidMain (string[] args) {NewDome9 (). Method (8); }}classdome9{Private intX=3; voidMethodintFA) { intY=6; classdome8{voidmethod2 () {System.out.print (x); System.out.print (a); System.out.print (y); } } NewDome8 (). METHOD2 (); }}
The inner class in the method can not access local variables in the method, why?