Well, so if I use static initialization blocks to access another static data inside another class:

public class StaticTestA {

    public static Queue queue = new LinkedList();


    static {
        System.out.println("A start");
        StaticTestC.queue.add("A");
        StaticTestA.queue.add("A");
        StaticTestB.queue.add("A");
        System.out.println("A stop");
    }
}

public class StaticTestB {

    public static Queue queue = new LinkedList();


    static {
        System.out.println("B start");
        StaticTestC.queue.add("B");
        StaticTestA.queue.add("B");
        StaticTestB.queue.add("B");
        System.out.println("B stop");
    }
}

public class StaticTestC {

    public static Queue queue = new LinkedList();


    static {
        System.out.println("C start");
        StaticTestC.queue.add("C");
        StaticTestA.queue.add("C");
        StaticTestB.queue.add("C");
        System.out.println("C stop");
    }
}

Result:

A start
C start
B start
B stop
C stop
A stop
[C, B, A]

[B, C, A]

[C, B, A]

Woot! :icon_cool: This thing is seriously cool! Setting this kind of thing up for the same result in Delphi was a real pain. But instead of throwing an error, Java solved it by executing concurrently.

But does anybody know if this is official or not? I fear it can just fail on other JREs...:S

Member Avatar for harsh2327

That is good one.

And it is neither an error nor a bug.
Static blocks are executed when the class is first loaded into the memory. When you start your program, if all the classes load into the memory simultaneously, all the static blocks will be executed simultaneously.

That is good one.

And it is neither an error nor a bug.

Yes, it's a feature, and a very cool one!:cool: I like Java!:)

That is good one.
Static blocks are executed when the class is first loaded into the memory. When you start your program, if all the classes load into the memory simultaneously, all the static blocks will be executed simultaneously.

Yes, but here static blocks are accessing static data in other classes. There is a good chance that the 1st class loads, and try to access another before it's loaded. I had even put in a list instance which have to be created before it can be accessed, and the program didn't fail even then. So I'm sure Java is smart and stalls the execution when it references data that haven't yet been created. Also the stopping order is reversed, so it looks like a stack... :idea:
On top of this I've recompiled and run the program on my two core processor at least 10 times, and the order is always the same, so the execution is not random parallel, but it's explicitly controlled.

And here is two things that is required for this to work:
- The classes can be loaded "simultenously" by the JRE.
- The JRE stalls execution instead of generating error when the code reference objects that have yet to be created.

This two things are very straightforward to assume, yet it usually fail in other languages. So on top of publishing this because I haven't find any example on google, I'm interested if this behaviour is common to all JRE, or only to Sun's ones, or maybe it works only on the WinXP one?!
So testing and JRE documentation links are welcome. Thanks!

Static initialisers are run "when the class is initialised", which is (some time) before any reference to it is used, so the behaviour you observed is required by the language definition, and should be consistent in its results across all JREs. You may find that the first class to be initialised may vary depending on hiow the classes are loaded, but the final state will be the same

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.