Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

java - How does marking a variable as final allow inner classes to access them?

An inner class defined inside a method cannot access the method's local variables unless these local variables are marked final.I've looked at other posts in stack-overflow and java code ranch but none of them seem to be exactly answering the question as to how marking variables final allows inner class to access local variables in the method.

class MyOuter {
    private String x = "Outer";

    void fly(final int speed) {
        final int e = 1;
        class FlyingEquation {

            public void seeOuter()
            {
                System.out.println("Outer x is " + x);
            }
            public void display()
            {
                System.out.println(e);// line 1
                System.out.println(speed);// line 2
            }
        }
        FlyingEquation f=new FlyingEquation();
        f.seeOuter();
        f.display();
    }       
    public static void main(String args[])
    {
        MyOuter mo=new MyOuter();
        mo.fly(5);
    }
}

Explanations I found to this :

local variables are stored on stack and as soon as the method call finishes the stack is popped up and local variables are inaccessible whereas final local variables are stored in data section of memory potentially allowing JVM to access them even after the end of the method call. Where is the data section of memory ? I believe all local variables final or not are stored on the stack.When the method is removed from the stack the final variable will be removed with it. Is it that the value in the final variable is stored with the object in the heap ?

It does not support non-final fields as they could be changed, either by the method or the class and this is not supported because in reality there are two different fields/variables.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Marking a local variable as final tells the compiler that its value is guaranteed not to change once it has been assigned. This makes it safe for the compiler to create a synthetic field inside the inner class and copy the value of the local variable into this synthetic field when the inner class instance is constructed. All reads of the local variable from inside the inner class code actually compile into reads of the synthetic field instead.

This trick wouldn't work for non-final variables as they might change after the inner class was instantiated, and the synthetic field would get out of sync with the original variable.

It's important to realise that all inner classes are tricks of the compiler - to the runtime JVM all classes (top-level, nested static, and inner) are treated the same.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...