method-local inner class cannot use variables declared within the method

醉酒当歌 提交于 2019-12-11 08:43:44

问题


Why a method-local inner class can't use variables declared inside the enclosing method except those marked final, i know that the variables declared inside the enclosing method might vanishes while the inner class instance remains valid, but what has changed when this variable/s is declared final?


回答1:


The reason is that it is specified in the Java Language Specification #8.1.3

Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final.

Also note that project lambda (java 8), which aims at introducing closures in java (to replace anonymous classes), introduces the notion of effectively final which will allow you to use a non final variable within a lambda expression as long as you don't modify it within the closure.




回答2:


When the variable is final, a copy is placed in the inner class. i.e. it still can't access the variable, but it has a copy it can use.

You can see these copies if you use reflection or with a debugger.




回答3:


The reason is [ Actually Conclusion from Where are Java final local variables stored? ] : final variables are copied by the compiler into a hidden member variable of the inner class that references it. This way, they are guaranteed to not change after the copy has been made.

Also might be : The Method-Local Inner Class which is on the heap and the variable which is on the stack have different scope. But if the local variable is marked by final, it is stored on the heap.




回答4:


Now at first i would like to light upon

Does final local variables get stored on a heap instead of stack ?

Explanation: Now after some researching in SO i found out that all local variable (final or not) stored into the stack and go out of scope when the method execution is over.

But about final variable JVM take these as a constant as they will not change after initiated . And when a inner class try to access them compiler create a copy of that variable (not that variable it self) into the heap and create a synthetic field inside the inner class, so even when the method execution is over it is accessible because the inner class has it own copy. Synthetic field are filed which actually doesn't exist in the source code but compiler create those fields in some inner classes to make those field accessible. In simple word hidden field.

So final variable also stored in stack but the copy that variable which a inner class have stored in heap.

So now think about it.The local variables of the method live on the stack and exist only for the lifetime of the method. We already know that the scope of a local variable is limited to the method the variable is declared in. When the method ends, the stack frame is blown away and the variable is history. But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an instance variable. Because the local variables aren't guaranteed to be alive as long as the method local inner class object is, the inner class object can't use them. Unless the local variables are marked final. And it is beneficial making variable final as it can remain like synthetic field.




回答5:


Final ensures that you won't lose the reference to that variable. You don't want your inner class to destroy or lose your reference since you might continue using it in the context where was declared.



来源:https://stackoverflow.com/questions/11903982/method-local-inner-class-cannot-use-variables-declared-within-the-method

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