When does static class initialization happen?

JavaStaticInitialization

Java Problem Overview


When are static fields initialized? If I never instantiate a class, but I access a static field, are ALL the static blocks and private static methods used to instantiate private static fields called (in order) at that instant?

What if I call a static method? Does it also run all the static blocks? Before the method?

Java Solutions


Solution 1 - Java

A class's static initialization normally happens immediately before the first time one of the following events occur:

  • an instance of the class is created,
  • a static method of the class is invoked,
  • a static field of the class is assigned,
  • a non-constant static field is used, or
  • for a top-level class, an assert statement lexically nested within the class is executed1.

See JLS 12.4.1.

It is also possible to force a class to initialize (if it hasn't already initialized) by using Class.forName(fqn, true, classLoader) or the short form Class.forName(fqn)


> When does static class initialization happen?

See above.

> When are static fields initialized?

As part of static class initialization; see above.

> If I never instantiate a class, but I access a static field, are ALL the static blocks and private static methods used to instantiate private static fields called (in order) at that instant?

Yes. (Modulo that nothing is truly instantaneous.)

> What if I call a static method? Does it also run all the static blocks? Before the method?

Yes and yes.


Note that it is possible to construct code where you can observe the default initialized value of a static field.


1 - The final bullet point was present in the JLS for Java 6 through Java 8, but it was apparently a mistake in the specification. It was finally corrected in the Java 9 JLS: see source.

Solution 2 - Java

Static fields are initialized during the initialization "phase" of the class loading (loading, linking and initialization) that includes static initializers and initializations of its static fields. The static initializers are executed in a textual order as defined in the class.

Consider the example:

public class Test {
   
   static String sayHello()  {
      return a;
   }
   
   static String b = sayHello(); // a static method is called to assign value to b.
                                 // but its a has not been initialized yet.
   
   static String a = "hello";

   static String c = sayHello(); // assignes "hello" to variable c
   
    public static void main(String[] arg) throws Throwable {
         System.out.println(Test.b); // prints null
         System.out.println(Test.sayHello()); // prints "hello"
    }
}

The Test.b prints null because when the sayHello was called in static scope, the static variable a was not initialized.

Solution 3 - Java

Yes, all static initializers are run before you access class first time. If it was any other way, I would call it a bug.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTony RView Question on Stackoverflow
Solution 1 - JavaStephen CView Answer on Stackoverflow
Solution 2 - JavanaikusView Answer on Stackoverflow
Solution 3 - JavaNikita RybakView Answer on Stackoverflow