Static block in java

Static block is called when class initialized. Which means whenever your class is loaded Static block will be called.You can also call it static initializer block. Its same like as constructor. But difference is that in static block you can initialize static members of class.

Example of static block

public class{
	private static List<String> items = new ArrayList<>();
	static {
		items.add("My SQL");

As you can see in above example that you can declare variable globally but you can’t initialize them inline. To initialize them inline we use Static block.

Why we use Static block instead of Constructor?

you cannot initialize initials in-line as an unmodifiable map because then you couldn’t populate it! You also cannot do this in a constructor because simply calling one of the modifying methods (add,put etc.) will generate an exception.