Static initializer Block
In Java, a static initializer block executes code before the object instance initialization. A static block is a block of code with a static keyword
static {
// definition of the static block
}
Static initializer block or static initialization block, or static clause are some other names for the static block. Static block code executes only once during the class loading. The static blocks always execute first before the _main() _ in Java because the compiler stores them in memory at the time of class loading and before the object creation.
A class can have multiple static blocks, and they will execute in the same order as they appear in the class:
public class Day55 {
static {
System.out.println("block 1");
}
static {
System.out.println("block 2");
}
public static void main(String[] args) {
System.out.println("Main Method");
}
}
Instance Initializer Block
As the name suggests, the purpose of the instance initializer block is to initialize the instance data members.
The instance initializer block looks just like the static initializer block, but without the static keyword:
{
// definition of the Instance initialization block
}
Static initializer blocks always execute before the instance initialization blocks because static blocks run at the time of class loading. However, the instance block runs at the time of instance creation. The Java compiler copies initializer blocks into every constructor. Therefore, multiple constructors can use this approach to share a block of code:
public class Day55 {
{
System.out.println("Instance initializer block 1");
}
{
System.out.println("Instance initializer block 2");
}
public Day55() {
System.out.println("Class constructor");
}
public static void main(String[] args) {
Day55 iib = new Day55();
System.out.println("Main Method");
}
}
So now using both to create a DateTimeFormatter
for DateTimeFormatterBuilder()
to get a customized DateTimeFormatter
intance and initialize it in static block and then use it to calculate today’s date in the instance initializer block and printing todays date just by creating an instance of the class.
|
|
now the main method
|
|
By initializing the instance as static and then making the class as Singleton to get already initialized instance when classloader loaded the classes