Patch: error->warning when assigning to final field

Per Bothner per@bothner.com
Sun Jan 14 13:41:00 GMT 2001


Mark Wielaard <mark@klomp.org> writes:

> It does violate JLS 4.5.4, 8.3.1.2, 15.14.1/2, 15.15.1/2, 15.26 and
> chapter 16 (see also the example in 6.5.6).

I'm sorry if my message was misleading.  Of course the compiler should
complain when compiling a .java source file to either native or .class.
(Javac does complain about this - at least I assume it does.)

However, this patch is for compiling .class files to source files.
The philosophy of Gcj is that the compiler should emit an error if it
sees something that violates the JVM spec.  In general, it should emit
an error if it sees something that would cause a Java verifier to
complain.  Any other valid bytecode must be accepted, even if it could
never be generated by javac.  Assigning to a final field outside an
initializer is neither prohibited by the JVM spec nor by Sun's
verifier.  Therefore, it must be allowed by Gcj.  However, emitting a
warning is reasonable, because such a .class file will not be portable.

So quoting from the JLS is irrelevant.

> It might also prevent some optimizations if final variables are suddenly
> not really final.

Not really, because the JVM spec *does* prohibit assigning to a final
field in classes other than defining class.  (This is checked for just
above the code I patched.)  So we can always determine if a final
variable "is not really final".
-- 
	--Per Bothner
per@bothner.com   http://www.bothner.com/~per/


More information about the Java-patches mailing list