Default constructor is a constructor that is created automatically when a new class is created. The default constructor does not take any arguments. The default constructor is a do nothing constructor which does not do any initialization or any other activity.

The default constructor will work only if the class does not include any explicit constructor.

For example,

 

public class Employee { }

 

public class MainApplication {

            public static void main(String[] args) {

// Calling the default constructor Employee()

                        Employee empObject = new Employee();

            }

}

This code will work fine. Now modify the Employee class as shown below:

public class Employee {

            int empId;

            String empName;

            //An explicit constructor created with two arguments

            public Employee(int empId, String empName) {

                        this.empId = empId;

                        this.empName = empName;

            }

 

}

Now that the Employee class has an explicit constructor defined, the earlier code in the MainApplication will throw an error. This is because, default constructor will not work when there is an explicit constructor defined in the class.