Open In App

Instance Control Flow in Java

Last Updated : 17 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

This article will explain how Instance Control Flow takes place whenever objects are created.

Instance Control Flow In Normal Class

Whenever we are executing a java .class file, 1st Static Control Flow will be executed. In the Static Control Flow, if we are creating an object the following sequence of steps will be executed as part of Instance Control Flow:

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

Example:

Java




// InstanceControlFlow class
class InstanceControlFlow {
 
    // initializing instance integer i = 10
    int i = 10;
 
    // first instance block
    {
        // call instance method (methodeOne())
        methodOne();
        System.out.println("First Instance Block");
    }
 
    // constructor
    InstanceControlFlow()
    {
        System.out.println("Constructor");
    }
 
    // main method
    public static void main(String[] args)
    {
        // create InstanceControlFlow class object
        InstanceControlFlow f = new InstanceControlFlow();
        System.out.println("Main method");
    }
 
    // instance method (methodOne())
    public void methodOne() { System.out.println(j); }
 
    // second instance block
    {
        System.out.println("Second Instance Block");
    }
 
    // initializing instance integer j = 20
    int j = 20;
}


Output

0
First Instance Block
Second Instance Block
Constructor
Main method

Explanation:

When the above program will be executed, firstly Static Control Flow mechanism will occur in which class object is created which causes the Instance Control Flow mechanism in working as the following sequence.

Firstly, it will identify the instance member from top to bottom. After identifying instance members in the first step, both the instance blocks are executed from top to bottom. At first, inside the first instance block, we call methodOne() method, and inside that method, we print the value of variable ‘j’. JVM print value of variable ‘j’ as 0 because it is just initialized and yet not assigned with actual value, then the second instance block is executed.

Then, at last, the execution of the constructor will take place.

Note: Static Control Flow is a one-time activity and it will be executed at the time of class loading, but Instance Control Flow is not a one-time activity for every object creation it will be executed.

Instance Control Flow In Parent To Child Relationship

Whenever we are creating a child class object the following sequence of events will be executed automatically:

  1. Identification of instance members from Parent to Child.
  2. Execution of instance variable assignments and instance blocks only in Parent class.
  3. Execution of Parent class constructor.
  4. Execution of instance variable assignments and instance blocks in Child class.
  5. Execution of Child class constructor.

Example:

Java




// Parent class
class Parent {
 
    // initializing instance integer x = 10
    int x = 10;
 
    // first instance block of Parent class
    {
        // call instance method (methodeOne())
        methodOne();
        System.out.println("Parent First Instance Block");
    }
 
    // constructor of Parent class
    Parent()
    {
        System.out.println("Parent Class Constructor");
    }
 
    // main method of Parent class
    public static void main(String[] args)
    {
        // create Parent class object
        Parent p = new Parent();
        System.out.println("Parent Class Main Method");
    }
 
    // instance method (methodOne())
    public void methodOne() { System.out.println(y); }
 
    // initializing instance integer y = 20
    int y = 20;
}
 
// Child class
class Child extends Parent {
 
    // initializing instance integer i = 100
    int i = 100;
 
    // first instance block of Child class
    {
        methodTwo();
        System.out.println("Child First Instance Block");
    }
 
    // constructor of Child class
    Child()
    {
        System.out.println("Child Class Constructor");
    }
 
    // main method of Child class
    public static void main(String[] args)
    {
        // create Child class object
        Child c = new Child();
        System.out.println("Child Class Main Method");
    }
 
    // instance method (methodTwo())
    public void methodTwo() { System.out.println(j); }
 
    // second instance block of Child class
    {
        System.out.println("Child Second Instance Block");
    }
 
    // initializing instance integer j = 200
    int j = 200;
}


Output

0
Parent First Instance Block
Parent Class Constructor
0
Child First Instance Block
Child Second Instance Block
Child Class Constructor
Child Class Main Method

Explanation:

When the above program will be executed, firstly Static Control Flow mechanism will occur in which a child class object is created which causes the Instance Control Flow mechanism in working as the following sequence.

Firstly, it will identify the instance member from Parent to Child. After identifying instance members in the first step, the instance blocks are executed in the Parent class from top to bottom. At first, inside the first instance block of Parent class, we call methodOne() method, and inside that method, we print the value of variable ‘y’. JVM print value of variable ‘y’ as 0 because it is just initialized and yet not assigned with actual value, then Parent class constructor is executed.

Secondly, the instance blocks are executed in the Child class from top to bottom. Inside the first instance block of the Child class, we call methodTwo() method, and inside that method, we print the value of variable ‘j’. JVM prints the value of variable j as 0 because it is just initialized and yet not assigned with the actual value.

Then At last Child class constructor is executed.

Note: Object creation is the most costly operation in java and hence if there is no specific requirement never recommended to create objects.

Direct and Indirect Reference

Inside instance block, if we are trying to read a variable that read operation is called Direct Read. Inside instance block, If we are calling a method and within that method, if we trying to read a variable that read operation is called Indirect Read.

Example:

Java




// DirectIndirectRead class
class DirectIndirectRead {
 
    // initializing instance integer i = 10
    int i = 10;
 
    // instance block
    {
        methodOne(); // -> Indirect Read
        System.out.println(i); // -> Direct Read
    }
 
    // instance method (methodOne())
    public void methodOne() { System.out.println(i); }
 
    // main method
    public static void main(String[] args)
    {
        // create DirectIndirectRead class object
        DirectIndirectRead d = new DirectIndirectRead();
    }
}


Output

10
10

1. RIWO (Read Indirectly Write Only) State

If a variable is just identified by the JVM and the original value, not assigned then the variable is said to be in RIWO[Read Indirectly Write Only] state. If a variable is in RIWO[Read Indirectly Write Only] state then we can’t perform Direct Read but we can perform Indirect Read. If we are trying to read directly then we will get a compile-time error saying:  

Example 1

Java




// RIWODemo1 class
class RIWODemo1 {
 
    // instance block
    {
        System.out.println(i); // -> Direct Read
    }
 
    // main method
    public static void main(String[] args)
    {
        // create RIWODemo1 class object
        RIWODemo1 r = new RIWODemo1();
    }
 
    // initializing instance integer i = 10
    int i = 10;
}


 
Output

prog.java:6: error: illegal forward reference
        System.out.println(i); // -> Direct Read
                           ^
1 error

Example 2

Java




// RIWODemo2 class
class RIWODemo2 {
 
    // instance block
    {
        methodOne();
    }
 
    // instance method (methodOne())
    public void methodOne() { System.out.println(i); }
 
    // main method
    public static void main(String[] args)
    {
        // create RIWODemo2 class object
        RIWODemo2 r = new RIWODemo2();
    }
 
    // initializing instance integer i = 10
    int i = 10;
}


Output

0

2. R & W (Read & Write) State

In R & W(read and write) state the JVM assigned their respective original values to the variables as mentioned in code.

Example:

Java




// RW class
class RW {
 
    // initializing instance integer i = 10
    int i = 10;
 
    // instance block
    {
        System.out.println(i);
    }
 
    // main method
    public static void main(String[] args)
    {
        // create RW class object
        RW r = new RW();
    }
}


Output

10


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads