initialization of final fields
Martyn Pearce
m.pearce@inpharmatica.co.uk
Thu Nov 4 06:55:00 GMT 1999
Jerry Kramskoy writes:
| Hi Martyn,
|| I agree that the write*should* be performed just once, but again, the
| pragmatic approach taken by Sun is as I stated to avoid penalising the
| runtime (interpreter/JIT'd code) ... so if gcj provides this, it will
| be offering something that probably no other Java runtime does.
Ah, but that we were talking of runtimes.
I believe that original suggestion (and I'm sure that my comments) were
directed at the compiler, not the runtime.
The JLS (1.0), 8.3.1.2 `final Fields' states that:
`Any attempt to assign to a final field results in a compile-time
error'. (paragraph 2)
http://java.sun.com/docs/books/jls/html/1.1Update.html
D.1.2 `New Uses for final' states:
`You can defer initialization of a final field or variable, as long as
you initialize it before it is used and assign a value to it exactly
once. The compiler will check for proper assignment, as will the
verifier before code is executed.' (paragraph 3)
So it seems that there's a case to be answered for how Sun's runtime
behaves, given that the jdk compiler does not adhere to Sun's
specification.
But then, if the jdk runtime is broken too, do we really want to follow
that for the sake of compatibility, or indeed, pragmatism. I'm inclined
to think that we should fix bugs, not clone them.
Mx.
More information about the Java
mailing list