Java Constructors and Constructor Overloading

A Java constructor establishes the starting state of a new object.

Lesson 37 of 127Intermediate14 min read

A Java constructor establishes the starting state of a new object. Constructor overloading offers several ways to supply that state without duplicating initialization rules. You will compare no-argument constructors, parameterized constructors and calls that delegate with this().

Review the related foundation lesson when you need a prerequisite. The complete programs below use Java 17-compatible syntax.

Constructor syntax

A constructor initializes a new object and has the same name as its class. It has no declared return type, including void.

A constructor uses the class name and has no return type, not even void. Writing void Ticket() declares an ordinary method instead. Constructors execute as part of object creation, and their parameters receive the arguments supplied after new. They cannot return a different object with a return expression. A factory method can do that job when object creation needs a more flexible interface.

Validation and distinct constructor overloads

A constructor can validate required state. Rejecting an invalid duration during creation prevents an object from starting in a broken condition.

Overloaded constructors must have distinct parameter lists. Different names for parameters do not distinguish signatures, and return types do not apply to constructors. Java resolves an overload from argument types at compilation. Provide meaningful alternatives, such as a title-only constructor and a title-plus-price constructor. Too many similar numeric overloads can make call sites hard to interpret because the arguments carry little visible meaning.

Delegation with this

Overloaded constructors accept different parameter lists. One can delegate to another using this(...) so the initialization rules stay in one place.

Delegating with this(...) lets a shorter constructor reuse a more complete constructor. For the Java 17 examples here, that invocation must be the first statement. The delegated constructor performs shared validation and field assignment, then control returns to the calling constructor. Constructor chains must terminate; a cycle of this calls is a compilation error. Choose one central path for the rules that every overload must enforce.

Superclass initialization

On Java 17, an explicit this(...) or super(...) constructor invocation must come first. A compiler-generated no-argument constructor exists only when no constructor is declared.

Java supplies an implicit no-argument constructor only if the class declares no constructors. Once you write a parameterized constructor, new Ticket() will not work unless you also declare a compatible no-argument constructor. A superclass must be initialized too. If you omit an explicit this or super invocation, Java 17 inserts super(), which fails if the superclass has no accessible no-argument constructor.

Running the examples

Save name.java, compile it with javac --release 17 name.java, and run java name. Use each program's own public class name and filename.

How RentalConstructors produces its output

Rental(String guide) delegates to Rental(String guide, int minutes) with sixty minutes. The central constructor rejects a nonpositive duration before storing guide and minutes. Creating standard with Maple therefore stores sixty, while creating extended with Willow and ninety uses the explicit duration. Printing both fields produces 60 / 90. The two overloads initialize separate Rental objects through one validation path.

How NamedTicket produces its output

The string argument reaches the constructor parameter title. this.title identifies the field in the new ticket, while the unqualified title on the right identifies that parameter. Because the field is final, the constructor must initialize it and ordinary later assignments are prohibited. This class has no declared no-argument constructor, so it cannot be constructed with empty parentheses.

How TicketDefaults produces its output

The one-argument call selects the shorter constructor, which delegates with a seat count of one. The two-argument call selects the longer constructor directly with three seats. Both paths assign the same fields in that longer constructor. Delegation does not construct a second TicketDefaults object: it initializes the object already being created.

Read an overload from its call site

Compare new Rental("Maple") with new Rental("Maple", 90). The first call expresses the default duration; the second chooses a custom duration. The constructor names are identical because they identify the same class, while their parameter lists distinguish the creation paths. Avoid a separate constructor that accepts invalid defaults and relies on a later setter to repair them. A useful exercise is to add Rental() delegating to the title-only overload, then trace the chain until it reaches the validated two-argument constructor. Count the objects created: a this call initializes the same rental, so the entire chain still creates only one object.

Java Summary

  • A constructor initializes a new object and has the same name as its class. It has no declared return type, including void.
  • A constructor can validate required state. Rejecting an invalid duration during creation prevents an object from starting in a broken condition.
  • Overloaded constructors accept different parameter lists. One can delegate to another using this(...) so the initialization rules stay in one place.
  • On Java 17, an explicit this(...) or super(...) constructor invocation must come first. A compiler-generated no-argument constructor exists only when no constructor is declared.
  • Continue with Java exercises, Java quizzes and the Java study roadmap.

Java Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Java Constructors and Constructor Overloading in a complete program

Java
public class RentalConstructors  {
    static class Rental  {
        final String guide;
        final int minutes;
        Rental(String guide)  {
            this(guide, 60);
        }
        Rental(String guide, int minutes)  {
            if(minutes <= 0) throw new IllegalArgumentException();
            this.guide=guide;
            this.minutes=minutes;
        }
    }
    public static void main(String[] args)  {
        Rental standard = new Rental("Maple");
        Rental extended = new Rental("Willow", 90);
        System.out.println(standard.minutes + " / " + extended.minutes);
    }
}
Output
60 / 90
Line-by-line explanation
  1. Line 17Evaluates standard.minutes + " / " + extended.minutes and prints 60 / 90.
Example 2

The declared constructor controls initialization

Java
public class NamedTicket  {
    private final String title;
    NamedTicket(String title)  {
        this.title = title;
    }
    public static void main(String[] args)  {
        NamedTicket ticket = new NamedTicket("Evening talk");
        System.out.println(ticket.title);
    }
}
Output
Evening talk
Line-by-line explanation
  1. Line 8Evaluates ticket.title and prints Evening talk.
Example 3

One overload supplies a default

Java
public class TicketDefaults  {
    private final String title;
    private final int seats;
    TicketDefaults(String title)  {
        this(title, 1);
    }
    TicketDefaults(String title, int seats)  {
        this.title = title;
        this.seats = seats;
    }
    public static void main(String[] args)  {
        TicketDefaults solo = new TicketDefaults("Talk");
        TicketDefaults group = new TicketDefaults("Talk", 3);
        System.out.println(solo.seats + ":" + group.seats);
    }
}
Output
1:3
Line-by-line explanation
  1. Line 14Evaluates solo.seats + ":" + group.seats and prints 1:3.

Java FAQ

Is a constructor an ordinary inherited method?

No. Constructors are not inherited or overridden, and they have no return type. A subclass declares its own constructors and calls an accessible superclass constructor as part of initialization. Method overloading and constructor overloading share signature ideas, but their roles remain different.

Can a constructor reject invalid arguments?

Yes. Throw an appropriate exception before storing invalid state. For example, reject a negative seat count with IllegalArgumentException in the central constructor, so every delegating overload follows the same rule. A constructor that throws does not deliver a successfully initialized object to the caller.

Should a constructor call other methods?

Calling private or final helper methods is fine. Avoid calling methods that a subclass can override: the override would run before the subclass's own fields are initialised, and it may see null or zero values. Keep constructors focused on validating and storing state.

What is a copy constructor?

A constructor that takes another object of the same class and copies its state, such as Rental(Rental other). It's a clear way to create an independent duplicate. Copy mutable fields, such as lists, rather than sharing them, or the copy and the original will change together.

When should I use a static factory method instead of a constructor?

A static method such as LocalDate.of(2026, 10, 11) or List.of(...) can have a descriptive name, can return a cached or shared object, and can return a subtype. Factories are useful when several constructors would have confusingly similar parameter lists, such as Temperature.fromCelsius and Temperature.fromFahrenheit.

Java Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Multiple choiceeasy

What happens after adding only Ticket(String title)?

Multiple choicemedium

Which constructors have different signatures?

Java Quiz

2 questions to check what you learned.

  1. What does this(title, 1) do in a constructor?

    • Delegates to another constructor of the same object
    • Creates a second object with new
    • Calls a static method named this
  2. For these Java 17 examples, where must this(...) appear?

    • As the first constructor statement
    • After all field assignments
    • Only inside main