How come making a local variable final would solve the compiler error issue inside the method local inner class in Java?
-
The SCJP book by Kathy Sierra says, However, the inner class object cannot use the local variables of the method the inner class is in. Why not? Think about it. The local variables of the method live on the stack, and exist only for the lifetime of the method. You 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, the inner class object can't use them. Unless the local variables are marked final! The following code attempts to access a local variable from within a method-local inner class. I can't understand the point of making a variable final. How come making it final solves the issue?
-
Answer:
The compiler could just allow access to local variables from an inner class; it could do so by copying the variables that are accessed from the inner class object from the stack into the that object. You can think of them as implicitly becoming (somewhat special) members of the inner class. But what if you create two objects, two instances of the inner class? What if one of these objects modifies the value of the variable? The other object would not see that change. That would be confusing, don't you think? The solution is to only allow such access on variables that cannot be modified. In that case, the behavior is clear and the implicit "magic" copying of the local variables does not cause confusion. So, no error is issued if the local variables you access from an inner class are declared final.
Daniel Kinzler at Quora Visit the source
Related Q & A:
- How to do a local mysql database replication on an online server?Best solution by howtoforge.com
- How to start a local Server on Windows Platform?Best solution by Stack Overflow
- How can we get SBJSON connectionDidFinishLoading method inside array data in another class?Best solution by stackoverflow.com
- How much does a local permit cost?Best solution by Yahoo! Answers
- How to insert a date variable in java?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.