RE: JavaMemoryModel: Semantics of final and fixing the immutable String problem

From: David Holmes (
Date: Tue May 15 2001 - 21:21:48 EDT

> In the string case, the reference to the array of characters and the
> offset have to be final.

Surely the length field must be final too, otherwise the wrong number of
characters may be read, or the wrong value returned from length()?

In the context of the example it is hard to see how the String and
StringBuffer classes would really be implemented. I would expect that using
copy-on-write, the String would be given a new char[] rather than the
StringBuffer (otherwise the String would keep alive the entire StringBuffer
char[], which could be much bigger than the string itself. In that context
the char[], length and offset can not be final because they are mutable
values. So it is difficult to view this example as a realistic example of
either the problem or the solution.

In general, I return to my earlier statement, that it seems to me that all
of the fields of the immutable object must be declared final. That seems
overly restrictive given the use of lazy initialisation and the problems of
final fields when used with serialization and cloning.

David Holmes
JavaMemoryModel mailing list -

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