OOP Basics ·
‹ Previous Next ›
⏱ 5 min read Modified: 2026-07-25

Java Constructor Types: Default and Parameterized

1. What is a constructor in Java

A constructor in Java is a special block of code that runs when an object is created and brings it into a valid initial state. Everything that follows the new keyword is the constructor call:

Box myBox = new Box();

A constructor has three distinguishing features:

  • its name matches the class name exactly;
  • it has no return type — not even void;
  • it can only be invoked through new (or from another constructor via this()/super()); you cannot call it by name like a regular method.

Let's add a constructor to the Box class right after the fields. The syntax looks like a method, but without a result type. Inside the constructor we assign the value 10 to every field:

public class Box {
    double width;
    double height;
    double depth;

    Box() {
        System.out.println("Constructing a Box object");
        width = 10;
        height = 10;
        depth = 10;
    }

    /**
     * Calculate the volume of the box
     *
     * @return volume
     */
    double getVolume() {
        return width * height * depth;
    }
}

Now every new Box() runs that code: two objects are created and both immediately get the dimensions 10 × 10 × 10.

public class BoxExample3 {
    public static void main(String[] args) {
        Box myBox1 = new Box();
        Box myBox2 = new Box();

        System.out.println("Volume: " + myBox1.getVolume());
        System.out.println("Volume: " + myBox2.getVolume());
    }
}
Constructing a Box object
Constructing a Box object
Volume: 1000.0
Volume: 1000.0

2. Default constructor

The earlier versions of the Box class declared no constructor at all, yet objects were still created. The reason is that if a class declares no constructor whatsoever, the compiler adds one for you — that is the default constructor. It takes no parameters, has the same access modifier as the class, and its body consists of a single implicit call to the superclass constructor:

public class Box {
    double width;
    double height;
    double depth;

    // The compiler inserts this constructor itself
    // when the class declares no constructor
    Box() {
        super(); // implicit call to the Object constructor
    }
}

As soon as you declare at least one constructor of your own — with or without parameters — the default constructor is no longer generated. That is exactly why, once Box(double width, double height, double depth) is added, the line new Box() stops compiling.

Worth clearing up

Textbooks often say that «the default constructor initializes all fields to their default values». That is not accurate: zeros, false and null are written by the JVM when memory for the object is allocated — before any constructor code runs. The generated constructor body itself is empty apart from super(). Keep the terms apart, too: a default constructor is created by the compiler, while a no-arg constructor is one you wrote yourself. Interviewers like that distinction.

3. Parameterized constructor

Like a method, a constructor can accept arguments. Such constructors are called parameterized constructors. They let you create an object already in the state you need, without a chain of setter calls.

Below is a constructor that takes three values and initializes the fields with them:

public class Box {
    double width;
    double height;
    double depth;

    /**
     * Box class constructor
     *
     * @param w box width
     * @param h box height
     * @param d box depth
     */
    Box(double w, double h, double d) {
        width = w;
        height = h;
        depth = d;
    }

    /**
     * Calculate the volume of the box
     *
     * @return volume
     */
    double getVolume() {
        return width * height * depth;
    }
}

Object creation now looks like this. The commented-out line will not compile: once your own constructor is declared, the class no longer has a no-arg constructor.

public class BoxExample4 {
    public static void main(String[] args) {
        Box myBox1 = new Box(10, 20, 15);
        Box myBox2 = new Box(3, 6, 9);
        // Box myBox3 = new Box(); // compilation error:
        // constructor Box in class Box cannot be applied to given types

        System.out.println("Volume: " + myBox1.getVolume());
        System.out.println("Volume: " + myBox2.getVolume());
    }
}

4. Constructor overloading

To get new Box() working again, simply add one more constructor to the class. Several constructors with different parameter lists is overloading - more details in lesson Method and Constructor Overloading in Java.

5. Calling this() and super()

A constructor can call another constructor: this(...) — a constructor of the same class, super(...) — a constructor of the superclass. Such a call is only allowed as the first statement of a constructor, and this(...) and super(...) cannot be used together in the same constructor. For a full walkthrough of the syntax, rules and common mistakes, see the dedicated lesson on the this keyword. A dedicated lesson on the super keyword is on its way — check back soon.

6. Constructor vs method

A constructor looks like a method on the surface but behaves differently. Here is the summary:

Aspect Constructor Method
Name Must match the class name exactly Any valid identifier
Return type None, not even void Required: a type or void
Invocation Only via new, this(...), super(...) By name, as many times as you like
Inheritance Not inherited, cannot be overridden Inherited, can be overridden
If you write none The compiler adds a default constructor Nothing is added
Modifiers public, protected, private or package-private Also static, final, abstract, synchronized

A constructor cannot be declared static, final, abstract or native: it takes no part in polymorphism and always works on the specific instance being created.

7. Where developers get tripped up

  • Turning a constructor into a method by accident. Write void Box() { ... } and the code compiles fine — but it is now an ordinary method named Box. Objects are built by the generated default constructor and your code silently never runs. The nasty part is that the compiler says nothing.
  • Forgetting this when names collide. width = width; inside a constructor does nothing useful — the field stays zero. Modern IDEs flag it as a warning.
  • Heavy logic inside a constructor. A constructor's job is to bring the object to a valid state. Database access, file reading and thread starts are better moved out or into a static factory method: a half-built object that blew up with an exception is painful to debug.
  • Expecting constructors to be inherited. Parent constructors do not appear in the subclass automatically — declare the ones you need and delegate through super(...).

One modern alternative is worth knowing: if a class only carries data, use a record (Java 16+) instead of hand-writing a constructor, equals() and hashCode(). The compiler generates the canonical constructor for you, and argument checks go into the compact constructor:

public record Box(double width, double height, double depth) {
    // compact constructor: validation only
    public Box {
        if (width <= 0 || height <= 0 || depth <= 0) {
            throw new IllegalArgumentException("Dimensions must be positive");
        }
    }

    double getVolume() {
        return width * height * depth;
    }
}

Frequently asked questions

Can a constructor return a value?

No. A constructor has no return type, and you cannot write void either. If you declare void Box() { }, the compiler reads it as an ordinary method named Box: the code compiles, but new Box() never calls it. This is a classic interview question.

What is the difference between a default constructor and a no-arg constructor?

The default constructor is generated by the compiler, and only when the class declares no constructor at all. A no-arg constructor is written by the developer and may contain any code. As soon as you add any constructor of your own, the compiler stops adding its own.

Are constructors inherited in Java?

No. Constructors are neither inherited nor overridden. However, a subclass constructor must call a superclass constructor, either explicitly through super(...) or implicitly through the super() the compiler inserts.

Why would you make a constructor private?

A private constructor prevents code outside the class from creating instances. It is used in utility classes full of static methods, in singletons, and when objects are handed out through static factory methods with descriptive names such as Box.ofCube(7).

Can a constructor throw an exception?

Yes, a constructor may declare throws and throw any exception. That is the normal way to stop an object with invalid data from existing: validate the arguments and throw IllegalArgumentException. In that case no reference is ever handed back to the calling code.

For more detail, see the official Oracle documentation.

Comments

Please log in or register to have a possibility to add comment.