Instance Control Flow in Java
Last Updated :
17 Dec, 2021
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:
- Identification of instance member from top to bottom.
- Execution of instance variable assignments and instance blocks from top to bottom.
- 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;
}
Output0
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:
- Identification of instance members from Parent to Child.
- Execution of instance variable assignments and instance blocks only in Parent class.
- Execution of Parent class constructor.
- Execution of instance variable assignments and instance blocks in Child class.
- 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;
}
Output0
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();
}
}
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;
}
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();
}
}
Similar Reads
Static Control Flow in Java
Static Control Flow decides the sequence of activities/steps that will be executed in order when we run a java class that contains static variables, methods, and blocks. This article will explain how static control flow occurs whenever a Java program is executed. Prerequisite: Static Blocks The Stat
4 min read
Using Instance Blocks in Java
The instance block can be defined as the name-less method in java inside which we can define logic and they possess certain characteristics as follows. They can be declared inside classes but not inside any method. Instance block logic is common for all the objects. Instance block will be executed o
3 min read
Static Control Flow with Inherited Classes in Java
Before understanding the static control flow of a program, we must be familiar with the following two terms: Class loading: It refers to reading the .class file and loading it into the memory(JVM). Java loads classes dynamically, that is, classes are loaded on demand only when they are referred.Clas
6 min read
IntStream count() in Java with examples
IntStream count() returns the count of elements in the stream. IntStream count() is present in java.util.stream.IntStream Syntax : long count() Example 1 : Count the elements in IntStream. Java // Java code for IntStream count() // to count the number of elements in // given stream import java.util.
2 min read
JavaFX | FlowPane Class
FlowPane class is a part of JavaFX. Flowpane lays out its children in such a way that wraps at the flowpane's boundary. A horizontal flowpane (the default) will layout nodes in rows, wrapping at the flowpane's width. A vertical flowpane lays out nodes in columns, wrapping at the flowpane's height. F
6 min read
Instance Methods in Java
Instance Methods are the group of codes that performs a particular task. Sometimes the program grows in size, and we want to separate the logic of the main method from other methods. A method is a function written inside the class. Since java is an object-oriented programming language, we need to wr
5 min read
Instance Variable Hiding in Java
One should have a strong understanding of this keyword in inheritance in Java to be familiar with the concept. Instance variable hiding refers to a state when instance variables of the same name are present in superclass and subclass. Now if we try to access using subclass object then instance varia
2 min read
instanceof Keyword in Java
In Java, instanceof is a keyword used for checking if a reference variable contains a given type of object reference or not. Following is a Java program to show different behaviors of instanceof. Henceforth it is known as a comparison operator where the instance is getting compared to type returning
4 min read
IntStream boxed() in Java
IntStream boxed() returns a Stream consisting of the elements of this stream, each boxed to an Integer. Note : IntStream boxed() is a intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a
2 min read
Collections in Java
Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read