Thursday, December 13, 2018

Java OOPS: Instance Control Flow

Hi All,

After explaining the static control flow , in this post I will explain how instance control flow , takes place in any java class.

Before we start , if you need to understand about the instance block , please take a look on this wonderful tutorial : Instance Initializer Block

Same as the static control flow , it also has some rules for executing. But in static control flow , it will automatically load by JVM , before the execution of Main Method because of static property. But in instance control flow , block/variables/methods will not execute until unless the object creation will not take place.Let's take a look with an example:

Example:

public class InstanceControlFlow {

int i =10;

{
m1();
System.out.println("First Instance Block!");
}

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

public static void main(String[] args){
System.out.println("Main Method");
}

public void m1(){
System.out.println(j);
}

{
System.out.println("Second Instance Block!");
}

int j =20;
}



Output :
Main Method


Explanation:
Unlike the static control flow , here we can see that because we are not creating any object so only main method with identify and main method execution will happen.So output is "Main Method"
only.

Now we will try same code , after creating the object in main method , to check again for result.

Whenever we are execute any 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 the part of Instance Control Flow

  • Rule 1: Identification of instance members from top to bottom 
  • Rule 2: Execution of instance variable assignments and instance blocks from top to bottom 
  • Rule 3: Execution of constructor 
So the base control flow will be like:
First Static Control Flow 
After that Instance Control Flow 

So Final Sequence will be :

  1. Identification of static members from Top to Bottom  (static control flow)
  2. Execution of static variable assignment and static blocks from top to bottom  
  3. (static control flow)
  4. Execution of main method  (static control flow)
  5. Identification of instance members from top to bottom   (instance control flow)
  6. Execution of instance variable assignments and instance blocks from top to bottom  (instance control flow)
  7. Execution of constructor    (instance control flow)
It means whenever any java class is going to be execute , first the static control flow will perform , then if object creation has been done , then instance control flow sequence would take place.

So first I am showing  the same above example but with object creation, after that I will explain the detail with sequences of execution with output.

public class InstanceControlFlow {

int i =10;

{
m1();
System.out.println("First Instance Block!");
}

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

public static void main(String[] args){
InstanceControlFlow instanceControlFlow = new InstanceControlFlow();

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

public void m1(){
System.out.println(j);
}

{
System.out.println("Second Instance Block!");
}

int j =20;
}

Here in above InstanceControlFlow instanceControlFlow = new InstanceControlFlow(); , due to this line , all instance member of this class will come in picture.

Now when we will execute this program , first static control flow will execute , then after the instance control flow i.e.,

-> First it will identify for static members from top to bottom [IN OUR EXAMPLE WE DON'T HAVE ANY]

-> Second compiler will execute of static variable assignment and static blocks from top to bottom
[IN OUR EXAMPLE WE DON'T HAVE ANY]

-> Third it will execute main method [IN OUR CASE FROM HERE EXECUTION WILL START ]

-> In Main Method the object has been created , so it will start our instance control flow . 

Let's take a brief look :





Now if we compile the code , we will get same output , as mentioned in above picture.

If again object has been created , then again output 1-4 will be repeated , let's take a look ,I have just changed the main method code in above example:

public static void main(String[] args){

InstanceControlFlow instanceControlFlow = new InstanceControlFlow();

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

InstanceControlFlow instanceControlFlow1 = new InstanceControlFlow();

}

Output :
0
First Instance Block!
Second Instance Block!
Constructor
Main Method
0
First Instance Block!
Second Instance Block!
Constructor

Hence we can see that , the number of object creation has been done in main method , the process will be repeated with same times.

Note: Static control flow is one time activity which we will be performed at the time of class loading. 
But instance control flow is not 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 object.

That's it. I would suggest first compile this code by yourself for more clarity. 



No comments:

Post a Comment