PDA

View Full Version : What is difference between static and init block in java?


rajesh
13-01-2010, 10:47 AM
The static block is only loaded when the class object is created by the JVM for the first time whereas init block is loaded every time class object is created. Also first the static block is loaded then the init block.

public class LoadingBlocks {

static{
System.out.println("Inside static");
}

{
System.out.println("Inside init");
}
public static void main(String args[]){
new LoadingBlocks();
new LoadingBlocks();
new LoadingBlocks();
}
}

Output will be :

Inside static
Inside init
Inside init
Inside init