Signup/Sign In

Static Block in Java

In Java, the static keyword is used for the management of memory mainly. the static keyword can be used with Variables, Methods, Block and nested class. A static block in a program is a set of statements which are executed by the JVM (Java Virtual Machine) before the main method. At the time of class loading, if we want to perform any task we can define that task inside the static block, this task will be executed at the time of class loading. In a class, any number of a static block can be defined, and this static blocks will be executed from top to bottom.

static block Image

Syntax:

	
static {
		**********
		**********
		// statements….
		**********
		**********
	}

	

Example of a static block

Static block executes before the main method while executing program. Statements written inside the static block will execute first. However both are static.

	
class StaticDemo1
{
	static
	{
		System.out.println("Welcome to studytonight.com");
		System.out.println("This is static block");

	}
	public static void main(String as[])
	{
		System.out.println("This is main() method");
	}
}
	

static-block-syntax Example

Example of multiple static blocks

When we have multiple static blocks then each block executes in the sequence. First static block will execute first.

	
class StaticDemo1
{
	static
	{
		System.out.println("Welcome to studytonight.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");

	}
}
	

multiple-static-block-syntax

Initializer Block in Java

In Java, the initializer Block is used to initialize instance data members. The initializer block is executed whenever an object is created. The Initializer block is copied into Java compiler and then to every constructor. The initialization block is executed before the code in the constructor.

Example:

	
class InitializerDemo1
{
	{
		System.out.println("Welcome to studytonight.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");
	}
}
	

initializer-block

Example using static and initializer block

We can have both static and initializer blocks in a Java program. But static block will execute first even before initializer block. See the below example.

	
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");
    }
}
	

static-and-initializer