Initialization test

John Keiser jkeiser at iname.com
Wed Oct 28 09:09:57 PST 1998


     You mean static initializer is not supposed to be called before static
fields and methods are accessed?  Frankly, I don't believe you.  That makes
so little sense it is ludicrous.  You would be accessing uninitialized
variables.  The spec says that that can happen in pathological cases (mostly
involving circular references from static initializers, in my experience),
but not in normal ones.
     Please give a reference to the spec you're talking about, Artur.  As
far as I can tell, several of your examples are wrong.  Have you tested this
with Sun's?  I'd be shocked if the Sun VM passed all these tests.  They
would be in violation of the JLS.
     Here are the initialization rules as I read them, reading from both
examples and text in JLS 12.4.1: static initializers should be called when
any constructor or method (static or otherwise) in the class is invoked, or
when a non-constant variable (static or otherwise) that is *not* constant is
used.  By constant, it means final, static, and initialized from the pool of
compile-time constants.  There is some reason that makes it so that this
variable will *always* have its initialized value and not the uninitialized
one.

     As to your tests:

     Class1 - static field access - This should *not* fail.  The accessed
field is not constant.
     Class2 - static field and method access - You are correct, this won't
fail.  I must ask, though, why you think that if both Class1 and Class3
should not cause initialization, that Class2 should.
     Class3 - static method access - This should *not* fail.  A method is
called.
     Class4 - compile-time constant field access - You are correct, this
*should* fail.  access to fields initialized with a compile-time constant
will not cause initialization.  This is the most useful test here and
probably should be included in various test suites.

     The rules of class initialization are not all that vague.  You just
need to read through both examples and text to get the full understanding.

--John Keiser



More information about the kaffe mailing list