Java Constructor Loading Tips

This is very interesting topic to discuses Java constructor loading. As a Java programmer, unfortunately, I haven't had a chance to go through in my experience because most of the time standard programming doesn't use this. However, it is very important to know that how the JVM load static/instance blocks and variables with the constructor in the initiation of new objects. I've wrote very simple program to illustrate this scenario.

public class Constructors {
        public static int count=0;
        
        public static void main(String[] args){
                
                System.out.println("["+ ++Constructors.count +"]"+"Instantiate");
                ChildClass c = new ChildClass();
        }
}

//super class
class SuperClass {
        
        //super class instance variable
        int varS =++Constructors.count;
         SuperClass(){
                System.out.println("["+ ++Constructors.count +"]"+"Super class default constructor");
                System.out.println("["+varS+"] Initialize super class variables");
        }
        
        //instance blocks 1
        {
                System.out.println("["+ ++Constructors.count +"]"+"Super class insance block - 1");
        }
        
        //instance blocks 2
        {
                System.out.println("["+ ++Constructors.count +"]"+"Super class insance block - 2");
        }
        
        //super class static block - 1
        static {
                System.out.println("["+ ++Constructors.count +"]"+"Super class static block - 1");
        }
        
        //super class static block - 2
        static {
                System.out.println("["+ ++Constructors.count +"]"+"Super class static block - 2");
        }
}

//sub class
class ChildClass extends SuperClass{
        
        //child class instance variable
        int varC= ++Constructors.count;
        
        //Default constructor
        ChildClass(){
                 //call another constructor
                 this(25);
                System.out.println("["+ ++Constructors.count +"]"+"Child class default constructor");
                System.out.println("["+varC +"] Initialize child class variables");
        }
        
        //another constructor
        ChildClass(int n){
                System.out.println("["+ ++Constructors.count +"]"+"call this(int 25) constructor");
        }
        
        //child class instance block one
        {
                System.out.println("["+ ++Constructors.count +"]"+"Child class instance block - 1");
        }
        
        //child class instance block two
        {
                System.out.println("["+ ++Constructors.count +"]"+"Child class instance block - 2");
        }

        
        //child class static block
        static {
                System.out.println("["+ ++Constructors.count +"]"+"Child class static block - 1");
        }
        
        //child class static block
        static {
                System.out.println("["+ ++Constructors.count +"]"+"Child class static block - 2");
        }
                
}

As shown in the above program, the output is as follows

>java Constructors
[1]Instantiate
[2]Super class static block - 1
[3]Super class static block - 2
[4]Child class static block - 1
[5]Child class static block - 2
[7]Super class insance block - 1
[8]Super class insance block - 2
[9]Super class default constructor
[6] Initialize super class variables
[11]Child class instance block - 1
[12]Child class instance block - 2
[13]call this(int 25) constructor
[14]Child class default constructor
[10] Initialize child class variables
>Exit code: 0

We can say that first super class static blocks and then child class static blocks are loaded. The main point is static blocks were executed as defined in sequence. As a second step, super class variables. First complete the supper class before child class is the policy.

Initialize super class instance blocks in the sequence of they have defined and then only complete the super class constructor.

First of all initialize the instance variables in the child class, next initialize with staring instance blocks in the define order and then invoke the default constructor which in turn call "this(,,)" constructor and complete it.

In conclusion, first initialize static blocks of both super and child classes, then complete the following for the super then same thing for the child

  1. Initialize variables
  2. Initialize instance blocks in their defined order
  3. Constructor which is called in the instance initialization ( with "new" keyword)
  4. Constrictors can be called from the constructor

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python