Java: Default Constructor with example

In this article, we will go through default constructor in detail with example

Default Constructor :

  • A constructor which takes no arguments is known as default constructor
  • Alternatively, it is also referred as no-arg constructor as it don’t takes any arguments

There are two ways to have default no-arg constructor in class :

  1. Explicitly defined by programmer inside class
  2. Implicitly inserted by compiler after compilation (when there is no constructor defined in class)

 

1. Explicitly defined by programmer inside class

  • In Employee class, default no-arg constructor is explicitly defined

Employee.java

package in.bench.resources.constructor.example;

public class Employee {

	// default constructor
	public Employee() {
		System.out.println("Employee class >> Inside default no-arg constructor\n");
	}

	// display() method
	void display() {
		System.out.println("display() method is invoked");
	}

	// main() method - entry point to JVM
	public static void main(String args[]) {
		Employee emp = new Employee();
		emp.display();
	}
}

Output:

Employee class >> Inside default no-arg constructor

display() method is invoked

Explanation :

  • Here in this example, Employee object is created using below line of code
  • Employee emp = new Employee();
  • Which invokes default no-arg constructor and prints the message inside constructor
  • And then using object ‘emp’ created, invokes member method display() which prints the message inside method

Now, let us move on and see an example based on default constructor inserted implicitly by the compiler

 

2. Implicitly inserted by compiler after compilation

  • In Employee class, there is no explicit default no-arg constructor defined
  • Still, below class compiles and executes successfully without any error
  • Example: Below line of code invokes a default constructor and still there isn’t any error
    Employee emp = new Employee();
  • Reason: Compiler inserts an empty no-arg default constructor after compilation

So during program execution Employee() constructor gets invoked and successfully new object ‘emp’ is created with default values like

  • 0 for int
  • null for String
  • false for Boolean
  • etc

Employee.java

package in.bench.resources.constructor.example;

public class Employee {

	// no explicit default constructor

	// display() method
	void display() {
		System.out.println("display() method is invoked");
	}

	// main() method - entry point to JVM
	public static void main(String args[]) {
		Employee emp = new Employee();
		emp.display();
	}
}

Output:

display() method is invoked

Error scenario:

  • In both the above cases, there is a default no-arg constructor either defined by the programmer explicitly or implicitly inserted by the compiler during compilation
  • So, invoking below line of code won’t throw any error in either case
    Employee emp = new Employee();
  • Reason: There is always default constructor defined in class whenever there is no constructor defined
  • But in case, if you invoke below line of code i.e.; constructor with one ‘int’ argument
    Employee emp = new Employee(19);
  • Then, compiler will throw an error stating below reason
  • Error: The constructor Employee(int) is undefined

1_Java_Default_Constructor_compilation_error

  • Solution: Define or add one more overloaded constructor with one ‘int’ argument, we will see more constructor examples in the upcoming article

Let us move on and explore parameterized constructor in detail

 

Read Also:

 

Happy Coding !!
Happy Learning !!

Java: Parametrized Constructor with example
Java: Constructor with example