Do the 'up to date' guarantees provided by final field in Java's memory model extend to indirect ref

Posted by mattbh on Stack Overflow See other posts from Stack Overflow or by mattbh
Published on 2010-05-13T22:26:06Z Indexed on 2010/05/13 22:34 UTC
Read the original article Hit count: 206

The Java language spec defines semantics of final fields in section 17.5:

The usage model for final fields is a simple one. Set the final fields for an object in that object's constructor. Do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished. If this is followed, then when the object is seen by another thread, that thread will always see the correctly constructed version of that object's final fields. It will also see versions of any object or array referenced by those final fields that are at least as up-to-date as the final fields are.

My question is - does the 'up-to-date' guarantee extend to the contents of nested arrays, and nested objects?

An example scenario:

  1. Thread A constructs a HashMap of ArrayLists, then assigns the HashMap to final field 'myFinal' in an instance of class 'MyClass'
  2. Thread B sees a (non-synchronized) reference to the MyClass instance and reads 'myFinal', and accesses and reads the contents of one of the ArrayLists

In this scenario, are the members of the ArrayList as seen by Thread B guaranteed to be at least as up to date as they were when MyClass's constructor completed?

© Stack Overflow or respective owner

Related posts about java

Related posts about final