Instance Control Flow - rahul00773/JavaConcepts GitHub Wiki

Whenever we are using executing a java class first static control flow will be executed. In the static control flow if we are creating an object the following sequence of events will be executed as a part of instance control flow.

1. Identification of Instance members from top to bottom.
2. Execution of Instance variable assignments and instance blocks from Top to bottom
3. Execution of constructor.

If required for providing test environment info and understanding what kind of whitelisting and details they required I also can join meeting.

public class InstaneBlockExample {

int i =10;

{

    m1();
    System.out.println("First Instance method");
}

InstaneBlockExample(){

    System.out.println("Constructor");
}

public static void main(String[] args){
    InstaneBlockExample instaneBlockExample = new InstaneBlockExample();  // Line 1

    System.out.println("Main");
    
}

public void m1()
{

    System.out.println(j);
}

int j =20;

}

// 0 First Instance block Second Instance Block Constructor Main

If we comment line 1 is Main.

Note: Static control flow is a one-time activity. Which will be performed at the time of class loading. But Instance control flow is not a one-time activity and it will be performed for every object creation.

Object creation is the most costly operation. If there is no specific requirement then it is not recommended to create an object.