Static and instance block in java

What  is static and instance block in java? What are the uses of them?

This is also a popular java interview question. Here are going to see how to define static and instance block and what are uses of these. Let’s see instance block.

Instance block – 

  1. Instance block is used to initialize instance data members.
  2. It will get invoked during object creation.
  3. Instance block and constructor both will get invoked during object creation but instance block will executed first.
  4. Instance block will get executed each time when we create object.
  5. Let’s see some example of instance block.

Example 1 –

package instanceblock;

public class InstanceBlock1 {

	{
		System.out.println("instance block");
	}

	public static void main(final String[] args) {
		final InstanceBlock1 instanceBlock1 = new InstanceBlock1();
	}
}

Output is – instance block

Example 2 – Instance block is getting executed before constructor

package instanceblock;

public class InstanceBlock1 {

	InstanceBlock1() {
		System.out.println("constructor will execute after instance block");
	}

	{
		System.out.println("instance block will execute before constructor");
	}

	public static void main(final String[] args) {
		InstanceBlock1 instanceBlock1 = new InstanceBlock1();
	}
}

Output is – instance block will execute before constructor
constructor will execute after instance block

 

Example 3 – Instance block will execute each time when we we create new object.

package instanceblock;

public class InstanceBlock1 {

	{
		System.out.println("instance block will execute each time during object creation");
	}

	public static void main(final String[] args) {
		 InstanceBlock1 instanceBlock1 = new InstanceBlock1();
		 InstanceBlock1 instanceBlock2 = new InstanceBlock1();
		 InstanceBlock1 instanceBlock3 = new InstanceBlock1();
		 InstanceBlock1 instanceBlock4 = new InstanceBlock1();
	}
}

Output is –

instance block will execute each time during object creation
instance block will execute each time during object creation
instance block will execute each time during object creation
instance block will execute each time during object creation

Static Block – 

  1. Static block will get executed only once during class loading.
  2. It is used to initialized static data members.
  3. It will get invoked before constructor or instance block.
  4. Let’s see some example for static block.

Example 1 –

package staticblockexample;

public class StaticBlockExample1 {
	static {
		System.out.println("static block");
	}

	public static void main(final String[] args) {

	}
}

Output is – static block

Example 2 – Here we will real time scenario where we can use static block.

First Scenario – Suppose you want to set DateTimeZone for entire application.

package staticblockexample;

public class StaticBlockRealTimeExample {

	// 1. first scenario
	static {
		// you might want to set timezone to UTC format for whole application
		// here DateTimeZone class is joda api
		DateTimeZone.setDefault(DateTimeZone.UTC);

	}
	
	public static void main(final String[] args) {

	}

}

Second Scenario – You want to load Driver class for JDBC connection.

package staticblockexample;

public class StaticBlockRealTimeExample {

	
	// 2. second scenario
	static {
		// you might want to load driver class time of class loading
		Class.forName("oracle.jdbc.driver.OracleDriver");  
	}
	
	

	public static void main(final String[] args) {

	}

}

Third Scenario – You may have some common and constant data which is going to use in whole application.

package staticblockexample;

import java.util.ArrayList;
import java.util.List;

public class StaticBlockRealTimeExample {

	// 3. third scenario
	private static final List<String> accountTypeList = new ArrayList<>();
	static {
		// suppose you have different type of account in your application
		// loan account,saving account, current account
		accountTypeList.add(0, "loanAccount");
		accountTypeList.add(1, "savingAccount");
		accountTypeList.add(2, "currentAccount");

	}

	public static void main(final String[] args) {

	}

}

What benefit we will get here, accountTypeList  will be available for whole application.