posted 18 years ago
Your guess is exactly right. The inner class doesn't access the local variables of the enclosing method directly: the compiler actually sets things up so that the inner class has member variables of the given types, and when the inner class instance is constructed, the local variables are copied into those members. If the local variables weren't final, you could tell that this trick was being used, so to avoid confusion, we have the rule about final variables.
Why is the trick needed? Well, besides the fact that the JVM has no mechanism for accessing local variables in another stack frame, there's the issue that the inner class instance may very well still exist long after the enclosing method has returned. If the enclosing method returned, those local variables would be gone, so the inner class needs to use a copy of its own.