Java study notes 23 --- internal class local internal class can only access final local variables, study notes 23 ---

Source: Internet
Author: User

Java study notes 23 --- internal class local internal class can only access final local variables, study notes 23 ---

A local internal class is a class defined in the method body or code block. It has been briefly introduced in note 19.

Today we will discuss why local internal classes can only access local variables that are constants.

 

Author: blog garden-Chan

Please respect the author's Labor achievements. for reprinting, please mark "reprinted" in the title and the original article link:

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

 

References:

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

 

1. First, let's take a 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;

The method defines a local internal class LoInnerClass, which implements the interface LoInClassIntf;

A final local variable a is also defined in the method, and a LoInnerClass object reference loInC is defined;

The Code is as follows:

1 // note 23: internal class -- local internal class -- Implementation interface, return internal Class Object 2 // interface 3 public interface LoInClassIntf {4 void test (); 5} 6 7 // method localInClassRe, return value is LoInClassIntf, local internal class to implement this interface, up transformation 8 public LoInClassIntf localInClassRe () {9 final int a = 1; // constant 10 11 // note 23 -- Internal class -- partial internal class -- Implement interface 12 class LoInnerClass implements LoInClassIntf {13 public void test () {14 System. out. println ("variable a:" + a); 15} 16} 17 18 LoInnerClass loInC = new LoInnerClass (); 19 return loInC; 20} 21 22 public static void main (String [] args) {23 // note 23 -- local internal class 24 Person per = new Person (); 25 26 LoInClassIntf lInCIntf = per. localInClassRe (); 27 lInCIntf. test (); 28}

 

Output result:

1 variable a:1

 

The memory in the execution of the member method localInClassRe is as follows:

 

The memory size after localInClassRe execution is as follows:

 

 

Analysis:

After the member method localInClassRe is executed, both the local variable a and the Object Reference loInC in the method body are released,But the objects allocated to the heap are not recycled. In this case, the local variable lInCIntf in the main method points to it.;

There is no problem yet, but in row 27th, lInCIntf calls the test method. The test method accesses the local variable a in the member method localInClassRe, And a does not exist at this time, therefore, an error occurs;

That is,The life cycle of local variables and objects of local internal classes is different;

To solve this problem, Java re-copies the local variables to be accessed by the local internal class and places the backup in the constant pool of the internal class, so that no matter whether the method is executed or not, if all copies exist, the error of accessing non-existent variables will no longer occur.

 

The memory in the execution of the member method localInClassRe is as follows:

 

The memory size after localInClassRe execution is as follows:

 

The local variable a mentioned above is defined in the method body. What if the local internal class accesses the parameters of the method body?

Java adopts the following method,By default, this parameter is passed in as a constructor for the local internal class. Then, this parameter is used to initialize variable a copied in the internal class.

 

How does the local internal class access local variables solved the problem, so why can only access final local variables?

 

2. Data Synchronization Problems

We copied a local variable to solve the problem of different lifecycles. What if the method body and the local internal class change the value of?

As shown in:

 

In this way, the data is not synchronized when the two a are inconsistent. Should a = 2 or a = 3 be used in the next step?

To solve this problem,Java requires that the local variables accessible to local internal classes must be final, that is, the internal class cannot change the value of the local variables to be accessed, so that data will not be synchronized.

 

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.