DEV Community

Cover image for Java Constructors Explained: Types, Rules, Overloading, and Examples
Rachit Joshi
Rachit Joshi

Posted on

Java Constructors Explained: Types, Rules, Overloading, and Examples

When learning Java object-oriented programming, classes and objects are among the first concepts you encounter. But creating an object is only part of the process—you also need a way to initialize its state.

This is where constructors in Java become important.

A constructor is a special block of code that is automatically invoked when an object is created. It is commonly used to initialize the object's instance variables and prepare the object for use.

What Is a Constructor in Java?

A constructor in Java is a special member of a class that is executed when an object is created using the new keyword.

For example:

class Student {
Student() {
System.out.println("Student object created");
}
}

public class Main {
public static void main(String[] args) {
Student s = new Student();
}
}

When new Student() is executed, the constructor is called automatically.

Unlike a normal method, a constructor does not have a return type, and its name must match the class name.

Rules for Creating a Constructor

There are several important rules to remember.

  1. Constructor Name Must Match the Class Name

If the class is called Student, its constructor must also be called Student.

class Student {
Student() {
System.out.println("Constructor called");
}
}

  1. Constructors Do Not Have a Return Type

A constructor must not have a return type, including void.

class Student {
Student() {
System.out.println("Valid constructor");
}
}

If you write void Student(), Java treats it as a method rather than a constructor.

  1. Constructors Cannot Be Certain Modifiers

Constructors cannot be declared static, final, abstract, or synchronized.

However, constructors can use access modifiers such as public, private, and protected.

Types of Constructors in Java

Constructors are commonly discussed in three forms:

Default or no-argument constructor
Parameterized constructor
Copy constructor

The first two are fundamental Java constructor patterns, while a copy constructor is a commonly used design pattern for creating an object from another object's values.

Default Constructor

A constructor that doesn't accept parameters is called a no-argument constructor.

class Bike {
Bike() {
System.out.println("Bike is created");
}
}

public class Main {
public static void main(String[] args) {
Bike b = new Bike();
}
}

If you don't explicitly define any constructor, the Java compiler provides a default constructor for the class.

This allows an object to be created even when the class doesn't contain an explicitly written constructor.

Parameterized Constructor

A parameterized constructor accepts values when an object is created.

class Student {
int id;
String name;

Student(int id, String name) {
    this.id = id;
    this.name = name;
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {
public static void main(String[] args) {
Student s = new Student(101, "Rahul");

    System.out.println(s.id);
    System.out.println(s.name);
}
Enter fullscreen mode Exit fullscreen mode

}

This approach is useful when every object needs to start with specific values.

Instead of creating an empty object and assigning values afterward, you can provide the required values during object creation.

Constructor Overloading

Java allows multiple constructors in the same class as long as their parameter lists are different. This is called constructor overloading.

class Student {
int id;
String name;

Student() {
    id = 0;
    name = "Unknown";
}

Student(int id, String name) {
    this.id = id;
    this.name = name;
}
Enter fullscreen mode Exit fullscreen mode

}

Here, the class has two constructors:

A no-argument constructor
A parameterized constructor

Java determines which constructor to call based on the arguments provided during object creation.

Constructor Chaining

Sometimes one constructor needs to call another constructor. This is known as constructor chaining.

Java provides this() for calling another constructor in the same class and super() for invoking a superclass constructor.

For example:

class Student {
int id;
String name;

Student() {
    this(0, "Unknown");
}

Student(int id, String name) {
    this.id = id;
    this.name = name;
}
Enter fullscreen mode Exit fullscreen mode

}

Here, the no-argument constructor calls the parameterized constructor using this().

Constructor chaining can help reduce duplicate initialization code and make classes easier to maintain.

Why Are Constructors Important?

Constructors make object initialization more organized and predictable.

They allow you to:

Set initial values for instance variables
Ensure objects start in a valid state
Accept required data during object creation
Provide multiple initialization options through overloading
Reuse initialization logic through constructor chaining

For example, a Student object can be created with an ID and name immediately instead of requiring several separate assignments.

Common Mistakes Beginners Make

When learning constructors, beginners often make a few mistakes:

Adding void
void Student() {
}

This is a method, not a constructor.

Using a Different Name
class Student {
Teacher() {
}
}

This doesn't define a constructor for Student.

Confusing Default and Parameterized Constructors

A no-argument constructor and a parameterized constructor serve different purposes. Understanding when each is appropriate makes object creation much easier.

Final Thoughts

Constructors are a fundamental part of Java OOP. They provide a clean way to initialize objects when they are created and support different initialization patterns through no-argument constructors, parameterized constructors, overloading, and constructor chaining.

If you're learning Java from the beginning, constructors are worth understanding well because you'll encounter them throughout Java programs and object-oriented design.

For a more detailed tutorial with additional examples and constructor concepts, check out the Java Constructor tutorial on TPointTech.

Top comments (0)