What is Class Loading and Static Blocks in Java? - GeeksforGeeks

13 Jan, 2021
Class Loading is the process of storing the class-specific information in the memory. Class-specific information means, information about the class members, i.e., variables and methods. It is just like that before firing a bullet, first, we need to load the bullet into the pistol. Similarly, to use a class first we need to load it by a class loader. Static block runs only once in the life of a class. It can only access the static members and will only belong to the class.
Static Block is just like any block of code beginning with a ‘static’ keyword is a static block. Static is a keyword which when attached to the method, variable, Block makes it Class method, class variable, and class Block. You can call a static variable/method using ClassName. JVM executes the static block at “CLASS LOADING TIME”.
Execution Order: For every static block, there is an order in which static block/method/variable gets initialized. 
Static BlockStatic VariableStatic Method
Static Block
Static Variable
Static Method
Now figuring out the connection between class loading and static block after having an idea over the static block and class loading, it is found that execution of a static block happens when a class gets loaded for the first time. It is a series of steps.
Illustration: Showcasing generic execution of that static block is supposed to happen with series of steps as mentioned.
Randomly considering a java file ‘File.java’, having a static block in it is followed by a series of steps as mentioned.
Compilation of java file.Execution of java file.Java virtual machine JVM is calling main method in the program.Class is loaded and all the necessary information is stored in memory by now.Execution of static block begins.
Compilation of java file.
Execution of java file.
Java virtual machine JVM is calling main method in the program.
Class is loaded and all the necessary information is stored in memory by now.
Execution of static block begins.
Example      
Java
// Java Program to illustrate static block concept// alongside discussing the class loading  // Importing all input output classesimport java.io.*;  // Classclass GFG {      // Static block    static    {        // Static block will be executed first        // before anything else          // Print message        System.out.println(            "I am static block and will be shown to eyeballs first no matter what");    }      // Main driver method    public static void main(String[] args)    {        // Print message        // Now main method will execute        System.out.println(            "I am the only line in main method but static block is hindering me to display first");    }}
I am static block and will be shown to eyeballs first no matter what
I am the only line in main method but static block is hindering me to display first

java-basics
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
Internal Working of HashMap in Java
Strings in Java