Static Block in Java
- Static Block Example in Java
- Static Block in Java 6
- Static Block in Java 7 or Later
- Static Block and Constructors in Java
- Multiple Static Blocks in Java
Java uses static blocks to execute code before object initialization. When we declare a block with a static
keyword, we call it a static block.
It is also known as a static initializer block or static initialization block in Java. The code inside the static block body executes once when the class is loaded into the memory.
Syntax:
static {
// body of the staic block
}
In Java, the static block is always executed before the main
method because it is stored in the memory at the class loading and before the object creation.
Static Block Example in Java
Let’s see this through the example. Here, we created a static block to check the execution sequence.
The static block executes first, even before the main()
method.
// Java Static Block Example
public class SimpleTesting {
// creating static block
static {
System.out.println("This is static block");
}
public static void main(String[] args) {
System.out.println("This is main method");
}
}
Output:
This is static block
This is main method
Here, JVM
executes the static block first, and after the complete execution of the static block, it executes the main()
method.
Here, a question may arise: Can we use static block without creating the main()
method? The answer is yes.
We can do JDK version 1.6
or previous; otherwise, it will display an error at runtime.
Static Block in Java 6
Here, we created a static block, and the class does not contain the main()
method. This code executes fine and displays the desired result.
public class SimpleTesting {
static {
System.out.println("This is static block");
}
}
Output:
This is static block
Static Block in Java 7 or Later
If we declare a static block in a class that does not have the main()
method, the code does not compile and throws an error to the console.
public class SimpleTesting {
static {
System.out.println("This is static block");
}
}
Output:
Error: Main method not found in class Example_1, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Let’s see one more example of a static block.
Here, we created a static block in a different class, and the main()
method is declared in another class.
The code is executed fine when accessing its variable with the class name. See the example below.
class Sample {
static int a;
int b;
// creating the static block
static {
a = 20;
System.out.println("Static block");
}
}
public class SimpleTesting {
public static void main(String[] args) {
// accessing a without creating an object
System.out.println(Sample.a);
}
}
Output:
Static block
20
Static Block and Constructors in Java
Note one point that the static blocks are executed before the constructors. See the example below.
class Sample {
static int a;
int b;
// creating the static block
static {
a = 20;
System.out.println("I am static block");
}
// creating the constructor
Sample() {
System.out.println("I am constructor");
}
}
public class SimpleTesting {
public static void main(String[] args) {
// creating the objects
Sample obj1 = new Sample();
Sample obj2 = new Sample();
}
}
Output:
I am static block
I am constructor
I am constructor
If we create 5 objects, then the instance block will execute 10 times, but the execution of the static block does not depend on object creation.
Its execution depends upon the class loading, and as we know, the class loads only one time so that the static block will execute only one time.
public class SimpleTesting {
// Declaring two instance blocks.
{ System.out.println("This is instance block-1"); }
{ System.out.println("This instance block-2"); }
// Declaring two static blocks.
static {
System.out.println("Static block-1");
}
static {
System.out.println("Static block-2");
}
// Declaring zero parameter constructor.
SimpleTesting() {
System.out.println("0 argument constructor");
}
// Declaring one parameter constructor with int parameter named a.
SimpleTesting(int a) {
System.out.println("1 argument constructor");
}
public static void main(String[] args) {
// Creating an object of class.
new SimpleTesting(); // Nameless object.
// Creating another object of class and passing an integer argument value.
new SimpleTesting(20); // Nameless object.
}
}
Output:
Static block-1
Static block-2
This is instance block-1
This instance block-2
0 argument constructor
This is instance block-1
This instance block-2
1 argument constructor
Multiple Static Blocks in Java
Java allows the creation of any number of static blocks in a class. We made two static blocks in a single class, and the code executed fine.
public class SimpleTesting {
static {
System.out.println("I am static block - 1");
}
static {
System.out.println("I am static block - 2");
}
public static void main(String[] args) {
System.out.println("I am main method");
}
}
Output:
I am static block - 1
I am static block - 2
I am main method
In the below example, we have defined a method
, constructor
, instant block
, static block
in a class.
See the order of execution to understand the JVM execution priority
.
public class SimpleTesting {
int x = 10;
static int y = 20;
void fun1(int a) {
System.out.println("This is instance method");
}
static void fun2(String str) {
System.out.println("This is static method");
}
SimpleTesting() {
System.out.println("0 argument constructor");
}
SimpleTesting(int a) {
System.out.println("1 argument constructor");
}
{ System.out.println("This is instance block"); }
static {
System.out.println("This is static block");
}
public static void main(String[] args) {
SimpleTesting at = new SimpleTesting();
SimpleTesting at2 = new SimpleTesting(10);
at.fun1(50);
SimpleTesting.fun2("Name");
}
}
Output:
This is static block
This is instance block
0 argument constructor
This is instance block
1 argument constructor
This is instance method
This is static method