Another reflection bug; compatibility results
Artur Biesiadowski
kaffe@rufus.w3.org
Wed, 14 Jun 2000 19:29:28 +0200
Stuart Ballard wrote:
> However, I think that asking for the value of a field, even a public
> static final one, is enough to cause <clinit> to get called... right? :(
It depends if it is compile time constant. If yes, then clinit will not
be called. From JLS 12.4.1
[when initialization occurs]
[...]
A non-constant field declared in T (rather than inherited from a
superclass or superinterface) is used or assigned. A constant field is
one that is (explicitly or implicitly) both final and static, and
that is initialized with the value of a compile-time constant expression
(§15.27). Java specifies that a reference to a constant field must
be resolved at compile time to a copy of the compile-time constant
value, so uses of such a field are never active uses. See §13.4.8
for a further discussion.
[...]
It seems that it is duty of compiler to not emit such calls. I wonder
what will happen if getstatic will be called anyway (by creating method
by hand). From JLS I understand it is illegal code and should be
verified away ?? Quite implementation dependent I'm afraid.
Artur