Java Learning Note---Local inner class of inner class can only access final local variables

Source: Internet
Author: User

A local inner class is a class that is defined in a method body or block of code that has been briefly described in note 19.

Today's discussion is about why local inner classes can only access local variables that are constants.

Blog Park-Cicada cicada

Please respect the work of the author, reproduced in the title of the word "reprint", and marked the original link:

Http://www.cnblogs.com/chanchan/p/8402411.html

Resources:

Http://www.cnblogs.com/dolphin0520/p/3811445.html

1. First, let's look at how local internal classes access local variables.

The person class is an external class, loinclassintf is an interface, Localinclassre is a member method of the person class, and the return value type is loinclassintf;

In the method, a local inner class loinnerclass is defined, and the inner class implements the interface loinclassintf.

The method also defines a final local variable A, which defines a loinnerclass type of object reference loinc;

The code is as follows:

1     //Note 23: Inner class--local inner class--implements interface, returns inner class object2     //Interface3      Public Interfaceloinclassintf {4         voidtest ();5     }6 7     //method Localinclassre, the return value is loinclassintf, the local inner class to implement the interface, the transformation upward8      Publicloinclassintf localinclassre () {9         Final intA = 1;//ConstantsTen          One         //note 23--Inner class--local inner class--implements interface A         classLoinnerclassImplementsloinclassintf { -              Public voidTest () { -SYSTEM.OUT.PRINTLN ("Variable A:" +a); the             } -         } -  -Loinnerclass LoInC =NewLoinnerclass (); +         returnLoInC; -     } +  A      Public Static voidMain (string[] args) { at         //note 23--local inner class -Person per =NewPerson (); -          -loinclassintf lincintf =per.localinclassre (); - lincintf.test (); -}

The output is:

1 variable a:1

The memory of the member method Localinclassre execution is as follows:

The memory of the member method Localinclassre after execution is as follows:

Analysis:

After the member method Localinclassre is executed, the local variable A and the object reference Loinc in the method body are freed, but the object allocated in the heap is not reclaimed, and the local variable lincintf in the main method points to it ;

There's no problem here, but in line 27th, lincintf calls the test method, and the test method accesses the local variable A in the member method Localinclassre, and a does not exist at this time, so an error occurs;

That is, local variables are different from the life cycle of the objects of local inner classes;

To solve this problem, Java re-copies the local variables to be accessed by local internal classes, and puts the backup in the constant pool of internal classes, so that the copy is present regardless of whether the method is executed or not, and there is no longer an error accessing the nonexistent variable.

The memory of the member method Localinclassre execution is as follows:

The memory of the member method Localinclassre after execution is as follows:

The local variable a referred to above is defined in the method body, if the local inner class accesses the parameters of the method body?

Java takes a method that defaults to the constructor of a local inner class, passing it as a parameter to the constructor method, and then using that parameter to initialize the copied variable A in the inner class.

The problem of how local internal classes access local variables is solved, so why is it possible to access final local variables only?

2. Data synchronization issues

By copying a local variable to solve the different life cycle problems, what happens if the method body and the local inner class change the value of a?

As shown in the following:

So two a inconsistent, there will be data out of sync, the next step should be used a=2 or a=3 it?

To solve this problem,Java rules that local internal classes can access local variables must be final, that is, the inner class can not change the value of the local variables to access, so there is no data synchronization problem.

Java Learning Note---Local inner class of inner class can only access final local variables

Related Article

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.