简体   繁体   中英

Java anonymous inner class

Does an instance of an anonymous inner class store all the accesible final variables or does it store only the variables that are being used inside the said inner class? (ie does it perform some kind of analysis? As far as I know the reference to the parent instance is being stored no matter what.)

The reference to the enclosing class is stored no matter what.

Final variables are stored as a copy, but only if you actually reference them somewhere in your inner class. This does not really required any extra "analysis" but can be done in the normal compile step (the compiler has to resolve the variable anyway, so it can output the code to set up the copy, too).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM