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.
public class{private static List<String> items = new ArrayList<>();static {items.add("Java");items.add("Android");items.add("PHP");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.
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.
Quick Links
Legal Stuff
Social Media