I can not figure out why this code:
class HelloWorld
{
HelloWorld()
{
System.out.println("1 cnstr ");
}
public static void main(String[] args)
{
HelloWorld a = new HelloWorld();
}
{
System.out.println("2 cnstr ");
}
}
gives me the output:
2 cnstr
1 cnstr
Why does line System.out.println("2 cnstr "); work? What kind of block is it? I used jdb and found that JVM starts block with this line in HelloWorld() constructor before any line in it.
Thanks for your help.
Its an Instance Initialization block. It is run before constructor of the class is executed.
From Documentation: