Method Local Inner Class Question
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.
Thanks,
Derek

