Showing posts with label Static. Show all posts
Showing posts with label Static. Show all posts

Friday, August 11, 2017

Execution flow of program by JVM - part 2

Program 2 – Involving static variable initialization, static block ,  instance variable initialization and constructor calling 

class Example {
     public static int step_1 = step(1);   <- CALLED First
     public int step_6 = step(6);

     static {
          step(2);                         <- CALLED Second       
     }

     public Example() {
          step(7);
     }

     // Just for demonstration purposes:
     public static int step(int step) {
          System.out.println("Step " + step);
          return step;
     }
}

public class ExampleSubclass extends Example {
     public static int step_3 = step(3);     <- CALLED THIRD
     public int step_8 = step(8);

     static {
          step(4);                          <- CALLED FOURTH
     }

     public ExampleSubclass() {
          step(9);
     }

     public static void main(String[] args) {
          step(5);
          new ExampleSubclass();
     }
}

Step 1, Step 2, Step 3, Step 4 is called during class loading by JVM before calling step(5)

Static initialization variables -->Static blocks --> Parent constructor invoke instance variable initialization --> Parent constructor invokes --> Child constructor invoke child instance variable initialization --> Child constructor invokes

Note- Static variables and static block of parent class and then child class is invoked at the starting of loading of the classes, even before execution of first line of main()

Output

Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8

Step 9

Execution flow of program by JVM- Part 1

Program 1- Execution Flow of a program involving static block, instance block, parent and child Constructor

/*Static block of parent is executed first because it is
loaded first and static blocks are called when the class is loaded.*/

class Parentt {

     /* parent static block called -1st*/
     static {
          System.out.println("Parent static block");
     }

     /* Parent Instance block called prior to
      * child IB once object is created -2nd */
     {
          System.out.println("Parent initialisation  block");
     }

     /*Parent constructor called prior to child constructor
      * once instance block is called - 3rd */
     public Parentt() {
          System.out.println("Parent Constructor");
     }

}

class Childd extends Parentt {

     /* child static block called after parent static block - 1st */
     static {
          System.out.println("Child static block");
     }

     /* Instance block called once object is created -2nd */
     {
          System.out.println("Child initialisation block");
     }
    
     /*Child constructor called once instance block is called - 3rd*/
     public Childd() {
          System.out.println("Child Constructor");
     }
}

public class ExecutionFlow {
     /* S(static blocks) -I(Instance Blocks) -Constructors */
     public static void main(String[] args) {
          new Childd();
     }
}

Output

Parent static block
Child static block
Parent initialization block
Parent Constructor
Child initialization block
Child Constructor


Tricky facts about Static variable

Facts about static and non-static variables

package com.javaserialization;

public class TestStatic {

     /* non-static variable can reference to static variable */
     static int c = 10;
     int d = c;

   /* static variable can't reference to non-static variable */

     int a = 10;
     static int b = a;

     public static void main(String[] args) {
          TestStatic test = new TestStatic();
          System.out.println(test.d);
     }
}