Static Block and its Usages in Java : Free Java Tutorials

Static block is a very important concept. Static blocks will be executed at the time of class loading. Hence, at the time of class loading, if we want to perform any activity, we have to define that inside a static block. We will see various usages of Static blocks in this tutorial.

For example, at the time of Java class loading, the corresponding native libraries should be loaded. Hence, we have to define this activity inside a static block.

Consider an example of usage of a static block in the database driver class. After loading every database driver class, we have to register driver class with the driver manager.

But, inside database driver class, there is a static block to perform this task and we are not responsible to do this registration explicitly.

Class DatabaseDriver
{
static
{
Register this driver with the driver manager
}

}

Within a class, we can declare any number of static blocks. But all these static blocks will be executed from top to bottom.

Without writing the main method, we can use a static block to print something on the console.

Example of a Static Block to print a statement on console

// Main Class
class StaticBlockDemo 
{
	// Static block
	static
	{
		// printing a statement from static block
		System.out.println("Statement is getting printed from static block without main method");

		// exiting the execution without waiting for main method
		System.exit(0);
	}
}

Running this program will print the statement that is inside a static block and then exit the execution.

Output:

Statement is getting printed from static block without main method

Also, there are multiple ways we can print something on the console without main method as well as static blocks.

Look at the following three different programs which will print the same statement on the console.

// Main Class
class StaticBlockDemo 
{
	//  assigning value to static variable
	static int var = method();
	
	// static method
	public static int method()
	{
		// printing statement on console
		System.out.println("This statement is getting printed without main method as well as static blocks");
		
		// exiting the execution
		System.exit(0);
		return 5;
	}
}

In the following program, we will use instance block to print the statement on the console.

// Main Class
class StaticBlockDemo 
{
	static StaticBlockDemo sbd = new StaticBlockDemo();
	{
		// printing statement on console
		System.out.println("This statement is getting printed without main method as well as static blocks");
				
		// exiting the execution
		System.exit(0);		
	}
}

Now, we will use a constructor to print the same statement on the console.

// Main Class
class StaticBlockDemo 
{
	static StaticBlockDemo sbd = new StaticBlockDemo();
	
	
	StaticBlockDemo()
	{
		// printing statement on console
		System.out.println("This statement is getting printed without main method as well as static blocks");
				
		// exiting the execution
		System.exit(0);		
	}
}

The output of above three programs:

This statement is getting printed without main method as well as static blocks

However, from 1.7 version onwards, the main method is mandatory to start a java program execution. Hence, above program will work only with the versions 1.6 and less.


Static Control Flow in Parent-Child Relationship

Whenever we are executing child class, the following sequence of events will be executed automatically as a part of static control flow.

  1. Identification of static members from parent to child. (static variables, methods, and blocks are considered static members)
  2. Execution of static variable assignments and static blocks from parent to child.
  3. Execution of child class’ static main method.

Look at the following example for better understanding.

// Parent class
class Parent
{
	// initializing parent class static integer var1 to be 5
	static int var1 = 5;

	// first static block of Parent
	static
	{
		// calling static method from Parent's first static block
		method();
		System.out.println("First Static Block of Parent");		
	}
	
	// static main method of Parent
	public static void main(String[] args)
	{
		// calling static method
		method();
		System.out.println("Parent class Main method is executing");				
	}
	
	// static method
	public static void method()
	{
		// displaying value of var2
		System.out.println(var2);
	}

	// initializing parent classs static integer var2 to be 10
	static int var2 = 10;
}


// Child class
class Child extends Parent
{
	// initializing child classstatic integer var1 to be 5
	static int var1 = 50;

	// first static block of Child
	static
	{
		// calling static method
		method();
		System.out.println("First Static Block of Child");		
	}
	
	// static main method of Child class
	public static void main(String[] args)
	{
		// calling static method
		method();
		System.out.println("Child class Main method is executing");				
	}
	
	// static method
	public static void method()
	{
		// displaying value of var2
		System.out.println(var2);
	}

	// second static block of Child
	static
	{
		System.out.println("Second Static Block of Child");
	}

	// initializing static integer var2 to be 10
	static int var2 = 100;
}

This program will run in three steps described above and produce following output:

0
First Static Block of Parent
0
First Static Block of Child
Second Static Block of Child
100
Child class Main method is executing

Note that, only the child class main method is executing here and parent class main method is skipped in the static control flow.

This is all about static blocks and its usages in Java. Hope you find this tutorial helpful.

Leave a Comment

Your email address will not be published. Required fields are marked *