[concurrency-interest] JLS: finality of "enclosing instance" of inner classes

Matthias Ernst matthias at mernst.org
Mon Mar 15 07:37:25 EDT 2010


does the JLS contain any language about the finality of the enclosing
instance reference? In practice, javac maps the "outer this" to a final
field "this$0", assigned in the inner class' constructor which would render
it safely published as of JLS 17.5 ("Final field semantics"). However, I
couldn't find any language in the spec regarding this.

I stumbled over this looking at how java.util.HashMap constructs its
collection views lazily:

  960       private Set<Map.Entry<K,V>> entrySet0() {
  961           Set<Map.Entry<K,V>> es = entrySet;
  962           return es != null ? es : (entrySet = new EntrySet());
  963       }


This is only safe if final field semantics apply to enclosing instances.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20100315/b02874d4/attachment.html>

More information about the Concurrency-interest mailing list