print
Advertisment
Advertisment

Instance Control Flow in java

Instance control flow is the process through which the Java Virtual Machine (JVM) executes the code inside a class. Understanding how the instance control flow works is important for anyone learning Java, as it is a fundamental aspect of the language.

When a class is loaded into memory by the JVM, it is initialized and its static block, if any, is executed. After the static block is executed, the instance control flow begins. In this phase, instance variables are initialized in the order they appear in the class definition, including any assignments and initializers.

Instance control flow is a sequence of steps that happen when a new object is created. It is executed in a specific order, and each step occurs only once per object. There are six different steps in the instance control flow process that occur in the following order:

1. Identification of Instance Members:

The first step is to identify all the instance variables and instance blocks that are present in the class. Instance variables are declared inside the class and outside any method, constructor, or block. An instance block is a block of code enclosed in curly braces ({}) that is not inside any method, constructor, or static block.

2. Execution of Instance Variables:

The instance variables are initialized with their default values or values provided by the programmer. If the programmer provides an initialization value, then that value is assigned to the variable. Otherwise, Java assigns the default value for the data type.

3. Execution of Instance Blocks:

The instance blocks are executed in the order they appear in the class. These blocks are executed only once per object, and they are used to initialize the instance variables.

4. Execution of Constructor:

After the execution of instance blocks, the constructor is executed. The constructor is a special method that is used to initialize the object. The constructor is called after the memory is allocated to the object and before the object is returned to the caller.

5. Execution of Instance Method:

After the constructor is executed, the instance methods can be called. These methods can access and manipulate the instance variables of the object.

6. Destruction of Object:

Finally, when the object is no longer needed, the garbage collector destroys the object and deallocates the memory associated with it.

Advertisment

Here is an example of a simple class with instance variables:

Example 1:

MyClass.java

public class MyClass {
    int myVar;
    {
        System.out.println("First instance block: myVar=" + myVar);
        myVar = 10;
    }
    MyClass() {
        System.out.println("Constructor: myVar=" + myVar);
    }
    {
        System.out.println("Second instance block: myVar=" + myVar);
        myVar = 20;
    }
    void myMethod() {
        System.out.println("myVar=" + myVar);
    }
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.myMethod();
    }
}

When an object of MyClass is created using the new keyword, the following instance control flow occurs:

  1. Any instance variable declarations are executed in the order they appear.
  2. Any instance blocks are executed in the order they appear.
  3. The constructor is executed.

In this example, there are two instance blocks, which are executed in the order they appear. The first instance block initializes myVar to its default value of 0, and then prints the value of myVar using System.out.println(). The output will be:

First instance block: myVar=0

Then, the second instance block initializes myVar to 20, and then prints the value of myVar using System.out.println(). The output will be:

Second instance block: myVar=10

Finally, the constructor is executed, which prints the value of myVar using System.out.println(). The output will be:

Constructor: myVar=20

After the instance control flow has been completed, the method myMethod() is called on the object obj, which simply prints the value of myVar. The output will be:

myVar=20
Advertisment

Example 2:

MyClass.java

public class MyClass {
    private static int myStaticVar = 10;
    private int myInstanceVar;

    // First static block
    static {
        System.out.println("First static block: myStaticVar=" + myStaticVar);
        myStaticVar = 20;
    }

    // Second static block
    static {
        System.out.println("Second static block: myStaticVar=" + myStaticVar);
    }

    // Constructor
    public MyClass(int myInstanceVar) {
        System.out.println("Constructor: myInstanceVar=" + myInstanceVar);
        this.myInstanceVar = myInstanceVar;
    }

    // First instance block
    {
        System.out.println("First instance block: myInstanceVar=" + myInstanceVar);
        myInstanceVar = 30;
    }

    // Second instance block
    {
        System.out.println("Second instance block: myInstanceVar=" + myInstanceVar);
    }

    // Main method
    public static void main(String[] args) {
        System.out.println("Main method");
        MyClass myObject = new MyClass(42);
    }
}

Output :

First static block: myStaticVar=10
Second static block: myStaticVar=20
Main method
First instance block: myInstanceVar=0
Second instance block: myInstanceVar=30
Constructor: myInstanceVar=42

Explanation :

  • First, the static block is executed when the class is loaded. In this example, there are two static blocks. The first static block prints the value of myStaticVar, which is initialized to 10. Then, it sets myStaticVar to 20. The second static block simply prints the value of myStaticVar, which is now 20.
  • After the static blocks, the main method is executed. This is the entry point for the program.
  • When an object of MyClass is created using the new MyClass(42), the instance control flow is executed. First, the first instance block is executed. It prints the value of myInstanceVar, which is initialized to the default value of int, which is 0. Then, it sets myInstanceVar to 30.
  • Next, the second instance block is executed. It prints the value of myInstanceVar, which is now 30.
  • Finally, the constructor is executed. It prints the value of the parameter myInstanceVar, which is 42. Then, it sets myInstanceVar to the value of the parameter.
Advertisment
Advertisment
arrow_upward