Why local inner classes and anonymous inner classes can only access the local variables of the final?

First, we look at an example of a local inner class:

class OutClass {
        private int age = 12;
 
        public void outPrint(final int x) {
                class InClass {
                        public void InPrint() {
                                System.out.println(x);
                                System.out.println(age);
                        }
                }
                new InClass().InPrint();
        }
}

There is an external and an internal class OuterClass class InClass, an inner class access methods outside of class in a local variable x, where, x must be final, otherwise it will error:

Cannot refer to a non-final variable x inside an inner class defined in a different method

Let's analyze this question:

Pursue its fundamental reason is that the life cycle of the variable domain of the role of lead;

First of all you need to know is this: internal and external classes are in the same class level, not because of internal class definitions will be completed with the implementation of the method is destroyed in the process.

Here a problem arises: When the method of the outer class, local variables will be destroyed, but the internal class object may exist (only when no one refer to it, will death). Here there is a paradox: internal class object to access a variable that does not exist. To solve this problem, it will be a copy of local variables within the class as a member variable, so that when the death of a local variable, inner classes can still access it, the actual visit is "copy" of local variables. This seems to prolong the lifetime of the local variables. We can experiment by decompiling the resulting .class file:
First Run javac OutClass.java compile the command line window will get two files: OutClass $ 1InClass.class, OutClass.class:

javap is a Java class file decomposition device, can decompile, java bytecode can view generated by the compiler.

Here we can then execute the command javap -privateOutClass $ 1InClass decompile, -private represent all classes and members of the show, after the execution will get the following result:

visible method of local variables actually does use the copy as a member variable inside the class.

The question arises: When copying a local variable within the class as a member variable, must ensure that these two variables are the same, that is, if we modify the member variables within the class, method local variables have to change with it, how to solve the problem?

Local variables will be set to the final, after initializing it, I will not let you go to modify this variable, local variable to ensure the consistency of the member variables and methods within the class. This is actually a compromise.

If the variable is the final time:

If the basic type, its value can not be changed, to ensure that the copy of the original value of the local variables is the same;

If a reference type, its reference can not be changed to ensure that the copy of the original variables refer to the same object.

This makes the copy of the local variable class established with internal consistency.
Original link: https://blog.csdn.net/sf_climber/article/details/78326984

Guess you like

Origin www.cnblogs.com/jichi/p/12327882.html