Static block vs. initializer block in Java? [duplicate]

They’re for two very different purposes: The static initializer block will be called on loading of the class, and will have no access to instance variables or methods. As per @Prahalad Deshpande’s comment, it is often used to create static variables. The non-static initializer block on the other hand is created on object construction only, … Read more

What is an initialization block?

First of all, there are two types of initialization blocks: instance initialization blocks, and static initialization blocks. This code should illustrate the use of them and in which order they are executed: public class Test { static int staticVariable; int nonStaticVariable; // Static initialization block: // Runs once (when the class is initialized) static { … Read more

Static Initialization Blocks

The non-static block: { // Do Something… } Gets called every time an instance of the class is constructed. The static block only gets called once, when the class itself is initialized, no matter how many objects of that type you create. Example: public class Test { static{ System.out.println(“Static”); } { System.out.println(“Non-static block”); } public … Read more