What is an “incompletely constructed object”?

The end of a constructor is a special place in terms of concurrency, with respect to final fields. From section 17.5 of the Java Language Specification:

An object is considered to be
completely initialized when its
constructor finishes. A thread that
can only see a reference to an object
after that object has been completely
initialized is guaranteed to see the
correctly initialized values for that
object’s final fields.

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.

In other words, your listener could end up seeing final fields with their default values if it examines the object in another thread. This wouldn’t happen if listener registration happened after the constructor has completed.

In terms of what’s going on, I suspect there’s an implicit memory barrier at the very end of a constructor, making sure that all threads “see” the new data; without that memory barrier having been applied, there could be problems.

Leave a Comment