What is Class Loading and Static Blocks in Java?
Last Updated :
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 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.
Example
Java
// Java Program to illustrate static block concept
// alongside discussing the class loading
// Importing all input output classes
import java.io.*;
// Class
class 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");
}
}
OutputI 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
Similar Reads
Illustrate Class Loading and Static Blocks in Java Inheritance Class loading means reading .class file and store corresponding binary data in Method Area. For each .class file, JVM will store corresponding information in Method Area. Now incorporating inheritance in class loading. In java inheritance, JVM will first load and initialize the parent class and then
3 min read
Class Loading and Static Blocks Execution Using Static Modifier in Java 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: There is an order in which static block/method/
3 min read
Constructor Overloading with Static Block in Java In Java, Constructor is a block of codes similar to the method that is used to initialize the objectâs state. A constructor is invoked at the time of object or instance creation. Each time an object is created using a new() keyword at least one constructor (it could be default constructor) is invoke
4 min read
Static Block and main() Method in Java In Java static block is used to initialize the static data members. Static block is executed before the main method at the time of class loading. Example:Java// static block is executed before main() class staticExample { // static block static { System.out.println("Inside Static Block."); } // main
2 min read
Difference Between Static and Non Static Nested Class in Java Nested classes are divided into two categories namely static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. A class can either be static or non-static in java. So there is a lot of difference between makin
4 min read
Java.lang.Class class in Java | Set 1 Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It
15+ min read