On Jul 14, 12:39 am, DYChen <
dong-yuan.c...@intel.com> wrote:
> This seems to imply an instance can only be created after the class
> initialization is completed.
It tells you the set of actions that trigger initialization. It does
not guarantee that initialization has completed before those actions
complete.
Consider this example:
public class Foo {
public static Foo mFoo;
static {
System.out.println("static init begin");
mFoo = new Foo();
System.out.println("static init end");
}
public static void main(String[] args) {
Foo foo = new Foo();
System.out.println("done" + mFoo);
}
public Foo() {
System.out.println("creating a foo!");
}
}
% javac Foo.java && java Foo
static init begin
creating a foo!
static init end
creating a foo!
doneFoo@4aad3ba4
The "new Foo" in main() causes class initialization of Foo. The class
initializer creates a new instance with "new Foo". The VM has to
choose between allowing the second allocation to complete while class
init is in progress, or throwing some sort of "not ready yet"
exception.
This isn't a problem unless the Foo constructor tries to use static
fields that haven't been initialized yet.