The local variable is allocated in the stack, and it will fall out of scope after testMethod()
. Making the variable final ensures that it is ok to just pass a reference to it to the anonymous class. If it was not final, a later assignment to it in testMethod()
could change the value later with confusing results. (The user might expect the later assigned value used, but that would be impossible).
A field of the parent class, however can be accessed through the parent reference of the anonymous class, so any later assignments can be handled without confusion.