Table of Contents
In this post, we will see about how to implement static block in java.
Here are some important points about static block in java.
Static block
is used for static initializations of a class- Static block is executed only once when either you create an object of the class or access
static
member of the class - Static block is executed before constructor of object is called.
Syntax
1 2 3 4 5 |
static{ // statements } |
Example
Let’s see with the help of example.
Create a class App.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; public class App { static int a; int b; static { a = 100; System.out.println("Static block is called"); } App(int b) { this.b=b; System.out.println("Constructor is called"); } } |
Create a Main class named StaticBlockMain.java
1 2 3 4 5 6 7 8 9 10 |
package org.arpit.java2blog; public class StaticBlockMain { public static void main(String[] args) { System.out.println(App.a); } } |
When you run above program, you will get below output:
100
As you can see, when we accessed static variable of class, static block was called.
Let’s see another example and change content of StaticBlockMain.java
as below.
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; public class StaticBlockMain { public static void main(String[] args) { App app=new App(300); System.out.println("Static variable: "+App.a); System.out.println("Instance variable: "+app.b); } } |
Output:
Constructor is called
Static variable: 100
Instance variable: 300
As you can see, static block is called before constructor of object is called.
You can not use instance variable in static block
.
This is similar to static method, you can not use instance variable in static block otherwise you will get compilation error with Cannot make a static reference to the non-static field b.
Use multiple static blocks
You can use multiple static blocks in class and they will called from top to down.
Let’s understand with the help of example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package org.arpit.java2blog; public class App { static int a; static int b; static int c; static { a = 100; System.out.println("Static block 1 is called"); } static { b = 200; System.out.println("Static block 2 is called"); } static { c = 300; System.out.println("Static block 3 is called"); } App() { System.out.println("Constructor is called"); } } |
Create main class StaticBlockMain.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; public class StaticBlockMain { public static void main(String[] args) { System.out.println("Static variable 1: "+App.a); System.out.println("Static variable 1: "+App.b); System.out.println("Static variable 1: "+App.c); } } |
Output:
Static block 2 is called
Static block 3 is called
Static variable 1: 100
Static variable 1: 200
Static variable 1: 300
When we first accessed static variable App.a
,all static blocks were called in top to down sequence.
That’s all about static block in java.