Why must the Java internal class reference local variables in the external class be final?

Source: Internet
Author: User

Today, I wrote a multi-threaded program and found that when the internal class is defined in the method, if the internal class calls the variable in the method, the variable must be declared as the final type and cannot be understood, later I thought it should be the cause of the life cycle, because the variables defined in the method are local variables. When I leave the method, the variables will become useless and will be automatically eliminated, the internal class will lose its role without leaving its method. It has a wider life cycle. The following example shows how to use it:

 

 

As shown in the example, an internal class TimerPrint is declared in the Outer class, and the method in this class references a local variable testTxt in method start.

Logically: This internal class appears inside a method, but during actual compilation, the internal class is compiled into Outer $ 1TimerPrint. class, which indicates that the external class method is at the same level as the internal class. In other words, non-final variables and internal classes have different lifecycles! After the start is called, non-final variables will disappear, and the internal class reference will be invalid!

Practice: The behavior of the java compiler is as follows (the precondition is that the variable is referenced in the internal class ):
If it is defined as final, the java compiler will generate a copy of an external variable in the internal class TimerPrint. It can ensure that the internal class can reference external attributes and ensure the uniqueness of values.

If it is not defined as final, compilation fails! (Jdk1.6 tested ). Because the compiler does not copy non-final variables, the variables referenced by the internal class are invalid!

 

The following describes the compiled bytecode:

Bytecode after external class Compilation:

 


Bytecode after internal class Compilation:

 

 

 


If the variable d in the external class is not referenced by the internal class, final is optional, and the java compiler will not perform special processing !!

Add a parameter d and define it as a non-final type. After compilation, the file is as follows:

Compiled external class:
 

Compiled internal class:


 

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.