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