Static Block in Java
Table of Contents
Static Block in Java
Static Block in Java
The static keyword in Java is primarily utilised for memory management. The static keyword can be used with nested classes, variables, methods, and blocks. A static block in a programme is a collection of statements that the Java Virtual Machine (JVM) runs prior to the main method. So If we want to perform a task at the time the class loads, we can define it inside the static block, and it will be carried out at that time. So Any number of static blocks may be defined in a class, and these static blocks will be executed in the order they are defined.
Syntax:
static {
**********
**********
// statements….
**********
**********
}
Example
Static block
When a programme is running, the static block is executed before the main method. The static block’s statements will be executed first. Both, though, remain static.
class StaticDemo1
{
static
{
System.out.println("Welcome to
onlineexamguide.com");
System.out.println("This is static block");
}
public static void main(String as[])
{
System.out.println("This is main() method");
}
}
Example
Multiple static blocks
When there are multiple static blocks, they all run in sequence. The initial static block is the first to run.
class StaticDemo1
{
static
{
System.out.println("Welcome to
onlineexamguide.com");
System.out.println("This is static block I");
}
public static void main(String as[])
{
System.out.println("**********************");
System.out.println("This is main() method");
}
static
{
System.out.println("**********************");
System.out.println("This is static block II");
}
static
{
System.out.println("**********************");
System.out.println("This is static block
III");
}
}
Initializer Block in Java
In Java, instance data members are initialised using the initializer Block. And Every time an object is created, the initializer block is called. So Every constructor receives a copy of the Initializer block after being copied into the Java compiler. The constructor’s code is executed after the initialization block.
Example:
class InitializerDemo1
{
{
System.out.println("Welcome to
onlineexamguide.com");
System.out.println("This is Initializer
block");
}
public InitializerDemo1()
{
System.out.println("Default Constructor
invoked");
}
public static void main(String as[])
{
InitializerDemo1 obj = new InitializerDemo1();
System.out.println("This is main() method");
}
}
Example using static and initializer block
A Java programme can contain both static and initializer blocks. However, the initializer block will not run before the static block. A good example is provided below.
public class one extends two {
static {
System.out.println("inside satic block");
}
one() {
System.out.println("inside constructor of child");
}
{
System.out.println("inside initialization block");
}
public static void main(String[] args) {
new one();
new one();
System.out.println("inside main");
}
}
class two{
static {
System.out.println("inside parent Static block");
}
{
System.out.println("inside parent initialisation block");
}
two() {
System.out.println("inside parent constructor");
}
}