Friday, 30 January 2015

why java anonymous class variable declared final?

when an object of the anonymous class is instantiated, copies of the final local variables and method parameters referred to by the object's methods are stored as instance variables in the object.

The methods in the object of the anonymous class really access those hidden instance variables.


Thus, the local variables and method parameters accessed by the methods of the local class must be declared final to prevent their values from changing after the object is instantiated.

No comments:

Post a Comment