Unlike C++, Java supports a special block, called static block (also called static clause) which can be used for static initializations of a class. This code inside static block is executed only once: the first time the class is loaded into memory. For example, check output of following Java program.
// filename: Main.java class Test { static int i; int j; // start of static block static { i = 10; System.out.println("static block called "); } // end of static block } class Main { public static void main(String args[]) { // Although we don't have an object of Test, static block is // called because i is being accessed in following statement. System.out.println(Test.i); } } |
Output:
static block called
10
Also, static blocks are executed before constructors. For example, check output of following Java program.
// filename: Main.java class Test { static int i; int j; static { i = 10; System.out.println("static block called "); } Test(){ System.out.println("Constructor called"); } } class Main { public static void main(String args[]) { // Although we have two objects, static block is executed only once. Test t1 = new Test(); Test t2 = new Test(); } } |
Output:
static block called
Constructor called
Constructor called
What if we want to execute some code for every object?
We use Initializer Block in Java
References:
Thinking in Java Book
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Static and non static blank final variables in Java
- Difference between static and non-static method in Java
- Difference between static and non-static variables in Java
- Understanding storage of static methods and static variables in Java
- Why non-static variable cannot be referenced from a static method in Java
- Understanding "static" in "public static void main" in Java
- Order of execution of Initialization blocks and Constructors in Java
- Nested try blocks in Exception Handling in Java
- Shadowing of static functions in Java
- Are static local variables allowed in Java?
- Assigning values to static final variables in Java
- Comparison of static keyword in C++ and Java
- Static class in Java
- Static vs Dynamic Binding in Java
- Static methods vs Instance methods in Java
- static keyword in java
- Final static variable in Java
- Static import in Java
- Static Control Flow in Java
- Static Block and main() method in Java
Improved By : ayushpratapsingh

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
