The class initialization order in Java defines the sequence in which fields, blocks, and constructors are initialized. In brief:
class Parent { static { System.out.println("Parent static"); } { System.out.println("Parent init"); } Parent() { System.out.println("Parent constructor"); } } class Child extends Parent { static { System.out.println("Child static"); } { System.out.println("Child init"); } Child() { System.out.println("Child constructor"); } } // new Child() -> what order?
The output will be:
First, the static blocks of parents, then the static blocks of children, followed by the non-static blocks and constructors in the order of inheritance.
Question: When will the static initializers in the class be executed:
class Ex {
static { System.out.println("static"); }
}
— upon creating the first instance or the first use of any static method/field?
Answer: The static initializer is executed at the first access to the class, including access to any static member (method/field), not just when creating an instance.
Story
In a banking application, a static connection pool was initialized in a static block, but due to accessing a static method before creating the first instance, the connection was not created yet. This led to a NullPointerException under load.
Story
The logging service depended on a non-static field that was initialized after the parent class constructor was called. Logs were written to a null logger, causing important error messages to be lost.
Story
When adding a new field with an initial value, the developer placed it after the initialization block: the block tried to use an uninitialized field, resulting in errors during application startup and a difficult search for the cause.