Question

It is said that static blocks in java run only once when that class is loaded. But what does it actually mean? At which point is a class loaded by JVM (Java Virtual Machine)?

Is it when the main method in that class is called? And is it that all the super-classes of the same class are also loaded when the main method starts execution?

Consider that A extends B and B extends C. All have static blocks. If A has the main method, then what will be the sequence of execution of static blocks?

Was it helpful?

Solution

This is described in the Execution section of the JLS. Namely:

Initialization of a class consists of executing its static initializers and the initializers for static fields declared in the class. Initialization of an interface consists of executing the initializers for fields declared in the interface.
Before a class is initialized, its direct superclass must be initialized, but interfaces implemented by the class need not be initialized. Similarly, the superinterfaces of an interface need not be initialized before the interface is initialized.

So in your example, the static block of the "topmost" class (C) runs first, then that of B, then the most-derived one.

See that documentation for a detailed description of all the steps that go into loading a class.

(Classes get loaded when they are first actively used.)

OTHER TIPS

I think the following example will solve all of your problems:

Before a class is initialized, its superclasses are initialized, if they have not previously been initialized.

Thus, the test program:

class Super {
        static { System.out.print("Super "); }
}
class One {
        static { System.out.print("One "); }
}
class Two extends Super {
        static { System.out.print("Two "); }
}
class Test {
        public static void main(String[] args) {
                One o = null;
                Two t = new Two();
                System.out.println((Object)o == (Object)t);
        }
}

prints:

Super Two false

The class One is never initialized, because it not used actively and therefore is never linked to. The class Two is initialized only after its superclass Super has been initialized.

For more details visit this link

Edit details: Removed confusing lines.

From the Java Language Specification:

Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables) declared in the class. Initialization of an interface consists of executing the initializers for fields (constants) declared there.

Before a class is initialized, its superclass must be initialized, but interfaces implemented by the class are not initialized. Similarly, the superinterfaces of an interface are not initialized before the interface is initialized.

The process is described in more detail in the Java Virtual Machine Specification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top