Static block in java is used for changing the default value of static variables, initializing static variables of the class, write a set of codes that you want to execute during the class loading in memory.
xxxxxxxxxx
Static block is used for initializing the static variables.
This block gets executed when the class is loaded in the memory.
A class can have multiple Static blocks
xxxxxxxxxx
Static block is run immediately its loaded into memory(ie.,when class is
loaded into class loader), not when class is
created like a constructer. Its mostly used to initialize static variables
created in a class.
xxxxxxxxxx
It is used to create another block before the main class.
every java codes runs here first.
Any code gets executed before the main class
static{
//code is placed between these brackets.
}
xxxxxxxxxx
// to create static block in java
static{
// code to be executed is placed here
}
// its placed inside class. its executed whenever the class is created not when its object is created or called.