Why external local variables or parameters that are accessed by the Java anonymous inner class need to be final decorated

Source: Internet
Author: User

Most of the time, a class is defined as a separate program unit. In some cases, a class is also placed within the internal definition of another class, which is called an inner class, and the class containing the inner class is also called an outer class.

Class outer{    private int A;    public class Inner    {        private int A;        public void Method (int a)        {            a++;    local variable            this.a++;    Inner class member variable            outer.this.a++;//outer class member Variable        }}    }        

A common practice is to write a method that returns a inner class object in outer

Public Inner Getinner ()
{ return new Inner ();}

Use inner classes in other classes:

Outer Outer = new Outer (); Outer. Inner Inner = Outer.getinner ();//Outer.Inner Inner = Outer.new Inner ();

Use of the static inner class:

Outer.Inner Inner = new Outer.Inner ();

Anonymous inner classes cannot access local variables in external class methods unless the variable is declared as the final type

1. The "Anonymous inner class" referred to here is defined primarily in the member method of its outer class, while the instantiated class is completed, and if it accesses a local variable in the member method, the local variable must be final decorated.
2. The reason is the difficulty of compiling a program: The life cycle of an inner class object exceeds the life cycle of a local variable . The life cycle of a local variable: When the method is called, the local variables in the method are created in the stack, and when the method call ends, the stack is retired, and the local variables are all dead. The inner class object life cycle is the same as other classes: from creating an anonymous inner class object, the system allocates memory for the object until it dies (garbage collected by the JVM) until no reference variable points to the memory allocated to the object. So it is quite possible that the Member method has been called over, the local variable has died, but the object of the anonymous inner class is still alive.
3. If an object of an anonymous inner class accesses a local variable in the same method, it requires that the local variables it accesses in the stack cannot "die" as long as the anonymous inner class object is still alive.
4. WORKAROUND: Anonymous Inner class objects can access local variables that are defined as final types in the same method . After the definition is final, the implementation of the compiler is that all final type local variables to be accessed by the anonymous inner class object are copied into one of the data members in the object. Thus, even if the local variable in the stack is dead, the value of the local variable defined as the final type is never changed, so the anonymous inner class object can access the final type's local variable after the local variable dies, because it copies one copy and always matches the value of the original local variable.

Finally,Java 8 is smarter: If a local variable is accessed by an anonymous inner class, the local variable is equivalent to automatically using the final decoration . In addition, the lambda expression of Java 8 is similar to that of having access to the final external variable but does not require a final decoration, but the variable cannot be re-assigned.

Resources:

Http://www.cnblogs.com/eniac12/p/5240100.html

Https://mp.weixin.qq.com/s/-2dGPhjbY7TKtR3Un31Kig (javaλ-expression)

Why external local variables or parameters that are accessed by the Java anonymous inner class need to be final decorated

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.