I know that a method local inner class object cannot use the local variables of the method the inner class is in. I have read the reason for this as:
"The local variables of the method live on the stack, and exist only for the lifetime of the method. The scope of the 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."
I am presuming that the author was intending to say that the method local inner class object could not use other method local object references as opposed to method local primitives. My question is: why? Do method local objects (including method local inner class objects) reside on the stack (as opposed to where we would normally expect them, on the heap)? I don't think that is the case because I don't believe the author would have made the reference to being able to store the method local inner class object in an instance variable if that was the case.
If all objects reside on the heap and all references to the objects live on the stack. And if the garbage collector only cleans up the objects on the heap that do not have a reference. Why does it matter if a method local inner class object uses a local variable of the method the inner class is in? Shouldn't the fact that the method local inner class object having a reference to a method local object prevent the method local object from being garbage collected and, thus, be available for use by the method local inner class object?
I hope this question isn't too confusing.
You are missing the point: the local class accesses the local variables of the method. When the method exists all its local variables--both primitive variable and reference variables--cease to exist, while an instance of the local class may live on for a long time.
What happens when an instance of the local class needs to use a local variable of a method that has exited a long time ago, a variable that does not exist? Java's way to solve this problem is requiring that the local variable is final, that it never changes, and using the constant value.
Yea, I figured that was a typo; it threw me off for a moment when I first read it. For the follow-up question, I was curious about the local variables themselves. When I first posted this question, I wasn't at a computer where I could try it. I have since returned to my machine and found that you can use local variables to initialize instances of a method local inner class. In essence, I figured it out for myself.
In any event, thanks for your help in clarifying my initial question. =)