Java Empty Constructor

Sheeraz Gul Apr 12, 2022
Java Empty Constructor

An empty constructor is required when we need to create a new instance via reflection by our framework. If we don’t create any other constructor with arguments for the class, we don’t need to create an empty constructor because one default will already be present.

This tutorial demonstrates the empty constructor in Java and how to use it.

Empty Constructor in Java

When creating a constructor, we must ensure that the constructor has the same name as the class and doesn’t return any value; an empty constructor is a constructor that doesn’t require any parameters, and a default constructor can also be the empty constructor.

An empty constructor provides us with the instance of an object. We might need to use setters to set the necessary properties for it.

The empty constructor can be used when we want to make sure that any instance created is always valid and the variables are initialized. We can use the empty constructor to define the default values.

Let’s see an example in Java code.

package delftstack;

class Delftstack {
    String employee_name;
	int id;

    public Delftstack() {
    	// Empty Constructor
    }

    public Delftstack(String employee_name, int id){
       this.employee_name = employee_name;
       this.id = id;
    }

	public void print() {
		System.out.println(toString());

	}
}

public class Main {

    public static void main(String[] args){

    	Delftstack Demo = new Delftstack();
    	System.out.println(Demo.employee_name);
    	System.out.println(Demo.id);
    }
}

The output will be:

null
0

The code above shows two Delftstack() constructors with no parameters and data, and that’s the empty constructor).

These constructors are mostly used as default constructors where we can set default data so that when there are no parameters passed, the default data is used.

Let’s see an example for the default constructor.

package delftstack;

class Delftstack {
    String employee_name;
	int id;

    public Delftstack() {
    	employee_name = "John";
        id = 10;
    }

    public Delftstack(String employee_name, int id){
       this.employee_name = employee_name;
       this.id = id;
    }
}

public class Main {

    public static void main(String[] args){

        Delftstack Demo = new Delftstack();
        System.out.println(Demo.employee_name);
        System.out.println(Demo.id);
    }
}

The code above converted the empty constructor into the default constructor.

Output:

John
10

In Java, the compiler’s default constructor is mostly inserted when there is no empty constructor defined.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Constructor