[jmm-dev] Guarantees for enclosing-instance references?

Niklas Matthies ml_jmm-dev at nmhq.net
Tue Mar 19 20:48:08 UTC 2024


Hi,

Are the references to enclosing (outer) instances of inner class 
instances guaranteed to behave as if they were stored in final fields 
of the inner class?

I had asked this very question way back in 2007 on the now-defunct 
javamemorymodel-discussion mailing list, where Jeremy Manson confirmed 
that these implicit references do NOT have final-field semantics 
according to the JLS. See also this earlier discussion on the Java 
Memory Model mailing list:
http://www.cs.umd.edu/~pugh/java/memoryModel/archive/2350.html.

As far as I can see in the current JLS, this hasn't changed. Is this 
correct? If so, are there any plans to add more guarantees?

Similar questions arise for lambda expressions, and for the references 
to/copies of (effectively-)final variables in the outer scope that are 
referenced from within the body of a lambda expression or a local 
class.

Thanks,
Niklas



More information about the jmm-dev mailing list