When the static
keyword is attached to a block of code, the block is called a static block.
class Main{
static {
System.out.println("Hello World");
}
}
main()
method.main()
method up to Java version 1.5.main()
method can be in a static block.class Main{static {System.out.println("Static Block 1");}static {System.out.println("Static Block 2");}public static void main(String[] args) {System.out.println("hello world");}}
Static Block 1
.Static Block 2
.hello world
.When the code is run, first the code in the static block is run then the main method is executed.