Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? (i.e. does it perform some kind of analysis? As far as I know the reference to the parent instance is being stored no matter what.)

like image 699
David Apltauer Avatar asked Jul 01 '26 02:07

David Apltauer


1 Answers

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).

like image 163
Thilo Avatar answered Jul 02 '26 16:07

Thilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!