I know that static blocks are initialized at the time the class is loaded and since the class is loaded only once in a program,they are initialized only once.
IIB (I
Java compiler injects initializer blocks at the beginning of your constructors (after calling a superconstructor). To give you a better understanding I've compiled the following class
public class Foo extends SuperFoo {
private String foo1 = "hello";
private String foo2;
private String foo3;
{
foo2 = "world";
}
public Foo() {
foo3 = "!!!";
}
}
and run it through a javap decompiler:
Compiled from "Foo.java"
public class Foo extends SuperFoo {
private java.lang.String foo1;
private java.lang.String foo2;
private java.lang.String foo3;
public Foo();
Code:
0: aload_0
1: invokespecial #12 // Method SuperFoo."":()V
4: aload_0
5: ldc #14 // String hello
7: putfield #16 // Field foo1:Ljava/lang/String;
10: aload_0
11: ldc #18 // String world
13: putfield #20 // Field foo2:Ljava/lang/String;
16: aload_0
17: ldc #22 // String !!!
19: putfield #24 // Field foo3:Ljava/lang/String;
22: return
}