Constructor rules

  • Same name as class name
  • this() or super() must be the first statement in constructor
  • either use this() or super() not both

Initialization order

  1. Static variable initializers and static initialization blocks, in textual order, if the class hasn’t been previously initialized.
  2. The super() call in the constructor, whether explicit or implicit.
  3. Instance variable initializers and instance initialization blocks, in textual order.
  4. Remaining body of constructor after super().

Default Constructor

  • The default constructor is the no-argument constructor automatically generated unless you define another constructor.
  • If you define at least one constructor, the default constructor is not generated.
  • It calls parent constructor by calling super() assuming no-arg constructor is present
  • If a parent class do not have a no-argument constructor, then the child class cannot have a default constructor since Java will not know how to call parent class constructor

super()

  • Used to call Parent class constructor
  • It is called automatically with no-argument if you do not call from the child class constructor
  • If arguments, then use it with arguments: super(arg1, arg2,...)

Constructor chaining

  • A subclass must call constructor of super class which in turn must call its super class all the way up to Object class
  • This is called constructor chaining and you need to be aware of it when there is a long line of class descent

this

  • refer to currently calling object
  • alternate constructor can be called using this(arg1, arg2,...)

static initializer

  • executes before object initialization/class loading
  • can have multiple static initializer block in single class in the order they appear
  • parent class static initializer executes first
public class StaticBlockExample {
 
    static {
        System.out.println("static block 1");
    }
    
    static {
        System.out.println("static block 2");
    }
 
    public static void main(String[] args) {
        System.out.println("Main Method");
        // static block 1
        // static block 2
        // Main Method
    }
}

instance initializer

  • executes before object initialization/class loading
public class InstanceBlockExample {
 
    {
        System.out.println("Instance initializer block 1");
    }
    
    {
        System.out.println("Instance initializer block 2");
    }
    
    public InstanceBlockExample() {
        System.out.println("Class constructor");
    }
 
    public static void main(String[] args) {
        InstanceBlockExample iib = new InstanceBlockExample();
        System.out.println("Main Method");
        // Instance initializer block 1
        // Instance initializer block 2
        // Class constructor
        // Main Method
    }
}

Copy Constructor

  • constructor that creates an object using another object of the same Java class
  • helpful when object has several fields or we want to make deep copy
  • Shallow copy example:
    • works if all fields are primitive or immutable
public class Employee {
    private int id;
    private String name;
 
    public Employee() {
        this.id = 1;
        this.name = "hello;
    }
 
    // copy constructor: input is same type as class
    public Employee(Employee employee) {
        this.id = employee.id;
        this.name = employee.name;
    }
}
 
public class Main {
    public static void main(String[] args) {
        Employee emp1 = new Employee();      // No-arg constructor
        Employee emp3 = new Employee(emp2);  // Copy constructor
    }
}
class Employee {
private:
    int id;
    string name;
 
public:
    Employee() {
        id = 1;
        name = "hello";
    }
 
    // Copy constructor
    Employee(const Employee &employee) {
        cout << "copy ctor called!!" << endl;
        id = employee.id;
        name = employee.name;
    }
};
 
int main() {
    Employee e1; // No-arg constructor
 
    Employee e2 = e1; // copy constructor called implicitly
 
    Employee e3(e1); // copy constructor called explicitly
 
    Employee e4; // default constructor
    e4 = e1; // No copy constructor called
 
    return 0;
}

Deep copy

  • Copy constructor
  • Cloneable interface
  • Using JSON Serialization and Deserialization
    • libraries: Jackson and Gson

new and newInstance()

  • newInstance can be used if you don’t know the class name before hand
// using new
var item = new Item("orange", 900);
 
// using newInstance()
var clazz = itemObj.getClass(); // or from Item.class
var ctor = clazz.getDeclaredConstructor(String.class, int.class);
var item = ctor.newInstance("orange", 900);