Inner class and local variables

試著忘記壹切 提交于 2019-11-27 05:16:22

The answer is the two are in different scopes. So that variable could change before the inner class accesses it. Making it final prevents that.

Local variables always live on the stack, the moment method is over all local variables are gone.

But your inner class objects might be on heap even after the method is over (Say an instance variable holds on to the reference), so in that case it cannot access your local variables since they are gone, unless you mark them as final

It's because the inner class inside a function actually makes a copy of the local variable because local variable may be destroyed after the function call while the instance of the local class still exists. To make the two copies of the local variable always have the same value(to make them seem identical), you have to declare the variable as final.

I think this is to prevent that the programmer would make mistakes. This way, the compiler reminds the programmer that the variable will not be accessible anymore when you leave that method. This is critical with looping. Imagine this code:

public void doStuff()
{
    InnerClass cls[] = new InnerClass[100];
    for (int i = 0; i < 100; ++i)
    {
        cls[i] = new InnerClass()
        {
            void doIt()
            {
                System.out.println(i);
            }
        };
    }
}

When the doIt() method is called, what will be printed? i changed while looping. To prevent that confusion, you have to copy the variable to a local variable or create the variable final, but then you will be unable to loop.

Your inner class is final and so you shouldn't be able to modify anything from inside your final entity.

According to Java memory model use of final variable guarantees that the final variable are always initialized. We get error when we try to use local variable without initialization. using final guarantees the inner class that local variable which is been used is initialized.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!