Because of the way inner classes are implemented. The
Java language tricks you into thinking that you've got direct access to local variables in method1(). You don't. What happens under the hood is that MyClass gets a private field
x. When an instance of MyClass is constructed, this field gets initialised with the current value of
x.
The problem is this. If the value of
x were allowed to change, the illusion of having direct access to local variables would break down, because MyClass' copy of
x get out of sync with the value in the enclosing method. It would be pretty hard to make sense of what is happening. This is why the compiler enforces that
x must be final, so the two (or more) copies that exist can never become inconsistent.
- Peter