static, block, java, syntax
// sample class
package staticblock;
public class StaticBlock {
static int num;
static String name;
static {
System.out.println("Call static block");
}
static {
System.out.println("Assign value to num");
num = 1;
}
static {
System.out.println("Assign value to name");
name = "jack";
}
public StaticBlock() {
System.out.println("Call constructor");
}
}
// test class
package staticblock;
public class StaticBlockTest {
public static void main(String[] args) {
StaticBlock sb = new StaticBlock();
}
}
Console Output
Call static block
Assign value to num
Assign value to name
Call constructor
Process finished with exit code 0