Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Object-Oriented Concepts

Java constructors

Java constructor rules: the default constructor, overloading, this() and super() chaining, validation, private constructors, copy constructors and records.

A constructor is a special member that runs when an object is created with new. It has the same name as the class, has no return type, and its job is to put the new object into a valid state: assign fields, check arguments and acquire whatever the object needs.

If you write no constructor at all, the compiler adds a public no-argument one (the default constructor), which only calls the superclass's no-argument constructor. As soon as you declare any constructor, the default one disappears.

Java
class Account {
    private final String owner;
    Account(String owner) { this.owner = owner; }
}

Try it

Java Overloading and this() chaining

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java super(), private constructors and factories

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
[modifier] ClassName([parameters]) [throws ...] {
    [this(...) | super(...);]
    // body
}
Rule Explanation
Name and return type Must match the class name; no return type, not even void.
Modifiers public, protected, package-private or private. No static, final, abstract or synchronized.
Overloading Any number, distinguished by parameter lists.
this(...) Calls another constructor of the same class.
super(...) Calls a superclass constructor. If you write neither, super() is inserted.
Exceptions May declare throws; a thrown exception means the object is never created.

Usage notes

  • this(...) and super(...) must be called first in older Java. Java 25 finalized flexible constructor bodies (JEP 513), which allows statements that do not use the object being built (such as argument validation) before the explicit call; reading this before it is still forbidden. Check your project's target version before relying on this.
  • Chain to one main constructor. Letting the shorter constructors delegate with this(...) keeps validation and assignment in one place.
  • Do not call overridable methods in a constructor. A subclass override runs before the subclass's fields are initialized and may see default values.
  • A subclass without a matching super constructor fails to compile. If the parent has only Vehicle(String), every child constructor must call it explicitly.
  • Private constructors are used for utility classes, singletons and static factories. Static factory methods have names and can return cached or subtype instances.
  • Constructors are not inherited. A subclass must declare its own.
  • Enums and records. Enum constructors are implicitly private; records can have a compact canonical constructor for validation (records).
  • Final fields must be assigned exactly once in every constructor path; the compiler checks this.
  • Prefer builders when there are many optional parameters, rather than a long telescope of overloads.