The static initializer block is a very interesting item in Java that unknown by most of the Java novice community; it is glossed over in Java books but none of the books really go into any sort of dept. Understanding “what it is” and “how it works” are two very important concepts about static initializers that every Java developer should know.

What Is It?

  • A block of code ‘{ }’ that runs only one time, and it is run the first time the Constructor or the main() method for that class is called (Sun calls this ‘when the class is initialized‘)

How Does It Work?

  • Just declare a static { } block inside your Java class and throw some code in it
  • No return statement
  • No access to this or super
  • Can throw Unchecked Exceptions only

What Can It Be Used For?

  • Loading drivers and other items into the namespace. (For example ‘Class.forName(”com.mysql.jdbc.Driver”)’)
  • Initialize your complex static members once (possibly for singletons/etc)
  • Logging
  • Creating/parsing prepared SQL statements

Time to Play

I have created 2 simple classes to play around with the static initializer class: Loader and Test:

class Loader {
    static final String theName = "The Loader";
    static {
        System.out.println("Loader.static");
    }
    Loader() {
        System.out.println("Loader.Loader()");
    }
}

class Test {
    static {
        System.out.println( "Test.static");
    }
    Test() {
        System.out.println( "Test.Test()");
    }
    public static void main( String [] args ) {
        System.out.println( "Test.main");
        Test t = new Test();
        System.exit(0);
    }
}

If we compile and run Test the output we see is:

Test.static
Test.main
Test.Test()

So we see that static block code is called before the main method is executed. Now, let’s define a Loader variable in the constructor and print out ‘Loader.name’.

class Test {
    static {
        System.out.println( "Test.static");
    }
    Test() {
        System.out.println( "Test.Test()");
        // Define a loader and print out the static name
        Loader l;
        System.out.println( Loader.theName );
    }
    public static void main( String [] args ) {
        System.out.println( "Test.main");
        Test t = new Test();
        System.exit(0);
    }
}

When we compile Test and run it, we now see this:

Test.static
Test.main
Test.Test()
The Loader

So, we can see that neither defining a class nor accessing static class members constitutes “initializing” a class (which would call the static block).

Hmm, ok well lets initialize a few instances of a Loader instead:

class Test {
    static {
        System.out.println( "Test.static");
    }
    Test() {
        System.out.println( "Test.Test()");
        // Make a few Loaders
        Loader l1 = new Loader();
        Loader l2 = new Loader();
        Loader l3 = new Loader();
    }
    public static void main( String [] args ) {
        System.out.println( "Test.main");
        Test t = new Test();
        System.exit(0);
    }
}

Now we compile and execute and we get the expected output:

Test.static
Test.main
Test.Test()
Loader.static
Loader.Loader()
Loader.Loader()
Loader.Loader()

The static block was called only once before the first time the constructor was called.

Well, What About Inheritance?

Let’s try it; we’ll have Test inherit from Loader.

class Test extends Loader {
    static {
        System.out.println( "Test.static");
    }
    Test() {
        System.out.println( "Test.Test()");
    }
    public static void main( String [] args ) {
        System.out.println( "Test.main");
        Test t = new Test();
        System.exit(0);
    }
}

Now if we compile and run Test we get the output:

Loader.static
Test.static
Test.main
Loader.Loader()
Test.Test()

Interesting, the static blocks get loaded first as usual, but the Loader class had it’s static block called first, which makes sense.

Static initializer blocks: interesting stuff.

1 Response to “Static Initializer Block”


  1. 1 Monesh Punjabi

    Brilliant. Just what I needed. Saved me the time of figuring this out myself.

Leave a Reply