# JavaMemoryModel: Changed my mind on final fields

From: Bill Pugh (pugh@cs.umd.edu)
Date: Wed Jul 21 2004 - 16:11:49 EDT

After thinking about it, I don't see a significant reason to stand in
the way
of people shooting themselves if they write their own wild and crazy
bytecode.

And it simplifies things in the JVM spec.

The problem is that is it hard to come up with any sanity check that
is efficient and guaranteed not to barf on obfuscated code.

So I propose the putfield spec should say:

> If the field is final, the putfield must occur in an <init> method of
> the class that declares that final field. Otherwise, an
> {\tt IllegalAccessError} is thrown.

However, the formal semantics would also say that

> A {\em freeze} action on a final field $f$ of an object $o$
> takes place when a constructor for $o$ in which $f$ is written exits
> normally.

This would mean that within a constructor, you can write to a final
field of an instance
other than the instance being constructed. However, if you do so you do
not get a corresponding
freeze action.

Bill

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

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