Take the 2-minute tour ×

In the following scenario ,

import A;
public class B{
    static A a;
        a = new A();

Is it possible that the static initialization block gets called before a is properly initialized? Note: A here is a logging framework.

share|improve this question
What do you mean by initialization of A (the class A or the instance of A that you have created)? That static block isn't going to be executed until something triggers the loading of the class B. –  Bhesh Gurung May 2 '13 at 4:36
Sorry.Corrected the question –  nikel May 2 '13 at 4:40

5 Answers 5

up vote 3 down vote accepted

In the case you mention above static block will be called before A is initialized as static block will be called when class loads (Class B in your case). So when you do


First class B will be loaded where static block is called with it(One time process in JVM) and then static method will be called.

Also Note that Importing statement to load the class does not load the class. It happens when yo do some operation on that class.

share|improve this answer
But since , the class "B" has imported "A" , it obviously will have a dependency. Why isn't "A" initialized 1st?Another question "A"gets loaded 1st right? Also , if the initialization of A takes very long time , how will I know that it has been initialized or not? –  nikel May 2 '13 at 4:42
Please see my updated answer –  M Sach May 2 '13 at 4:45
@nikel import is only for the benefit of the compiler - it doesn't affect when classes get loaded. –  Paul Bellora May 2 '13 at 4:47

First, it is possible, but not certain, that the static initializer would be called before A is "properly initialized." The static initializer will be executed when B is is loaded by the classloader (see #9: http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.2). We don't have enough information from what you've given to know when that will happen relative to A being loaded (the constructor invocation in B's static initializer might be the first time A is loaded, or it might have happened long before).

Second, it's entirely likely that an instance of A being initialized will trigger the initialization of A in a way such that everything will work itself out. Keep in mind that A's constructor won't execute until the class is loaded, which would include running any static initializers for A. So I'm not sure what type of initialization you're worried about that might not happen.

Third, the import statement has nothing to do with any of this. It would behave the same whether you fully qualified com.foo.A used an import statement.

Finally, it would probably be helpful if you provided a real example. If A is just a logging framework, then it's not anything proprietary and you'll probably get a more helpful answer based on what will really happen with that specific framework.

Edit: see the link provided in the comment below for a concrete example.

share|improve this answer
+1 This example might be useful - was going to use it in an answer, but you covered everything I was going to say. –  Paul Bellora May 2 '13 at 4:49
It is not possible. –  EJP May 2 '13 at 4:54
We don't know what he means by "properly initialized." For all I know, there's some other process that has to create a log file in a certain place on the filesystem. The classloader issues are explained in various answers here, but it seemed clear to me that he wasn't just concerned with the classloading implications, and that there was something else at play as well. Maybe I'm wrong about what he's asking. –  Shaun May 2 '13 at 4:57
Thanks for the response.Got the point. And its proprietary because , the library has a few wrappers over the logging framework:) –  nikel May 2 '13 at 4:57
@EJP - it >>is<< possible if you have a dependency cycle in the static initialization code. And it is legal Java too. In this case, one class may observe the state of another classes statics in their "pre-initialized" state. This can even apply to static final fields ... though not to fields that are compile-time constant. –  Stephen C May 2 '13 at 5:31

Imports have nothing to do with it. There are no imports at runtime.

Referenced classes are loaded during the linking phase, which precedes the initialization phase. In this case A is loaded during the link resolution step for B, before B's static initializer executes.

Reference: JVM Specification: Loading, Linking, and Initializing.

share|improve this answer

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.


In order to that it is not possible that the static block gets called before A is properly initialized.

I think there would be no confusion if you initialize your static instance like -

static A a = new A();

share|improve this answer
Using a proprietary which recommends that can cause an issue... –  nikel May 2 '13 at 4:46

static A a; static{ a = new A(); } what this code clearly mean is code a = new A(); will be called and initialized only once and remain in permanent generation when class is loaded till main thread exit from the system.

share|improve this answer

Your Answer


