Monday, June 16, 2008

Why inner classes can access only local final variables?


Why inner classes can access only local final variables?

Local Inner Classes and Anonymous Inner Classes can access the local final variables of the block of code they have been defined into. This block of code is typically a method.

The reason why the access has been restricted only to the local final variables is that if all the local variables would be made accessible then they would first required to be copied to a separate section where inner classes can have access to them and maintaining multiple copies of mutable local variables may lead to inconsistent data. Whereas final variables are immutable and hence any number of copies to them will not have any impact on the consistency of data.

Note: All the inner classes will continue to have access to all the members of the enclosing class. No inner class instance can exist without the corresponding enclosing class instance.



Share/Save/Bookmark


2 comments:

Anonymous said...

Thanks that was Geek Explain ;)

Anonymous said...

Thanks. It was nice explanation. https://www.webtechdesk.com