Friday, 7 February 2014

Java Static Block , Init Block,Constructor execution order

Java Static Block , Init Block,Constructor execution order in java program.

Execution order.

First - static block will execute only one time for program execution.
Second -Init block - will call each time object creation . execute before the constructor call.
Third - Constructor will execute.

Example Program.

/**
 * This program is explains the execution order of Static,Init,constructor
 * @author Balamurugan.B
 *
 */
public class StaticBlockEx{
 //init Block
 {
  System.out.println("Am init ");
  
 }
 //static Block
 static{
  System.out.println("Am static");
 }
 //Constructor 
 public StaticBlockEx() {
  System.out.println("Am Constructor");
 }
 
 
 
 public static void main(String[] args) {
  //Object Creation
   StaticBlockEx obj = new StaticBlockEx();
   
  
  
 }
}

Output:

Am static
Am init 
Am Constructor


No comments:

Post a Comment