The inner class in the method can not access local variables in the method, why?

Source: Internet
Author: User

/** 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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.