Re: JavaMemoryModel: Access to final members by super through overloaded methods

From: Jeremy Manson (jmanson@cs.umd.edu)
Date: Tue Feb 22 2005 - 16:45:59 EST


Victor --

It is unclear what you mean by "would something like this work". In
your case, the answer is that final fields are initialized after the
call to super(). The program you gave below will initially invoke
super(), which calls writeMembers(). writeMembers() will print:

null
-10

because the integer "value" is compiled as a constant. When control
returns to the subclass initializer, it will initialize the map, and print:

{}
-10

It is generally important _not_ to access final fields before they have
been initialized. It is also important to ensure that other threads do
not access the final fields of an object before the end of the object's
constructor.

For example, if this code wrote out a reference to the SubClass object
before the end of the object's constructor, and that reference was read
by another thread without synchronization, then that other thread might
not read the correctly initialized final fields. This is true
regardless of whether the final fields had been written out when the
reference to the object was written.

                                        Jeremy
-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:01:09 EDT