Class Loading and Static Blocks Execution Using Static Modifier in Java
Last Updated :
29 Oct, 2021
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/variable gets initialized.
- Static Block
- Static Variable
- Static Method
Static Blocks are called even before the main method which is nothing but a static method i.e. execution point of every class.
Note:
Sometimes, it is asked in interviews, to print "HELLO" without printing it inside main method or calling any method from main. Answer is to use Static block as they get initialized before main so you can use them to print anything without having any dependency on main Method in java.
Java
// Class Loading and Static Blocks
// Execution Using Static Modifier in Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
// Created 3 Threads
MyThread myThread1 = new MyThread();
myThread1.start();
MyThread myThread2 = new MyThread();
myThread2.start();
MyThread myThread3 = new MyThread();
myThread3.start();
}
}
class MyThread extends Thread {
// Static Variable count is maintained across
// all threads as it is only created once
static int count = 0;
// Non Static Variable count is maintained separately
// for separate thread As non-static variables are
// created for every thread
int notStatic = 0;
public void run()
{
count++;
notStatic++;
System.out.println("Thread = " + count);
System.out.println("Non Static Variable Value="
+ notStatic);
}
}
OutputThread = 3
Thread = 2
Non Static Variable Value=1
Thread = 1
Non Static Variable Value=1
Non Static Variable Value=1
Static Variable
You can use Static Variables to save memory or in an operation where you want all threads to maintain one variable instead of having a different variable for every thread.
Static Method
Used when methods are more relevant to class than an instance of a class. Math Class in java is a great example in Java. It has all the static Methods which you can call using a class name like max, min, pow as these functions do not require different objects to have different values. You can pass parameters and get answer using className.methodName() .
Static Block
Used when you want to initialize any data before the execution control goes to other methods like the main method.
Rules:
- Static Methods can only call other static methods i.e. you can not call a non-static method from a static method like the main method.
- Static block can use the only static variable directly
Singleton Design Pattern is the Most Widely used design pattern where only one instance of the class is created and returned for every instance request of class. The static keyword is used to make object creation only once as static occupy memory only once and hence synchronization is maintained for every call.
Below is the example which clearly displays the order of execution. Static Block is called first even it is written after the main method. It proves Static Blocks are the first thing to get called even before the main method.
Java
// Class Loading and Static Blocks
// Execution Using Static Modifier in Java
import java.io.*;
class GFG {
// Static Variable
public static void main(String[] args)
{
System.out.println("Static Variable=" + count);
System.out.println("Static Method");
}
// Static Variable
static int count = 3;
// Called even before Main Method
static { System.out.println("Static Block"); }
}
OutputStatic Block
Static Variable=3
Static Method
Real-Life Example: To append output to the same log file, one logger object is created, and using logger.info() data can be appended in order of insertion.
Similar Reads
What is Class Loading and Static Blocks in Java? 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
3 min read
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
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
Static and Non Static Blank Final Variables in Java In Java, a variable provides us with named storage that our programs can manipulate. In Java, a blank final variable is a final variable that is declared but not immediately initialized. It must be assigned exactly once before use. When a variable is declared with static, it becomes a static blank f
6 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
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