this() or super() must be the first statement in constructor
either use this() or super() not both
Initialization order
Static variable initializers and static initialization blocks, in textual order, if the class hasn’t been previously initialized.
The super() call in the constructor, whether explicit or implicit.
Instance variable initializers and instance initialization blocks, in textual order.
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 }}
newInstance can be used if you don’t know the class name before hand
// using newvar item = new Item("orange", 900);// using newInstance()var clazz = itemObj.getClass(); // or from Item.classvar ctor = clazz.getDeclaredConstructor(String.class, int.class);var item = ctor.newInstance("orange", 900);