skip to content

Default & Parameterized Constructors

The no-arg constructor the compiler inserts for you, and the fact that it disappears the moment you declare any constructor of your own. A quick question with a real consequence for frameworks and subclasses that rely on a no-arg constructor.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What is a constructor in Java, and what is the implicit default constructor?

level: juniorimportance: must knowfreq 80%

answer

  1. same name as class, no return type
  2. default = compiler-inserted, no-arg, calls super()
  3. only when you write ZERO constructors
  4. access modifier matches the class
  5. frameworks need a no-arg ctor

basics

~20 s

A constructor is a special method that runs when you create an object with new, to set it up. If you write no constructor, Java adds a hidden no-argument one for you so you can still write new MyClass().

solid answer

~40 s

A constructor is a special block of code that runs once when an object is created with `new`. It has the same name as the class and no return type, and its job is to initialize the new object's fields. If a class declares no constructor at all, the Java compiler automatically inserts an implicit default constructor: a public, no-argument constructor whose body just calls `super()` (the superclass no-arg constructor). This is why you can write `new ArrayList()` even though you never see a constructor in your own empty class. The key rule: this freebie only appears when you declare zero constructors yourself. The moment you write any constructor, the compiler stops generating it.

go deeper

for a junior

Knows a constructor runs on new, shares the class name, has no return type, and that an empty class still works because the compiler adds a no-arg one.

for a middle

Can explain that the default ctor is public-matching-class, no-arg, body is super(), and is generated only when no constructor is declared.

for a senior

Connects the default ctor to framework requirements (JPA/Jackson) and the bytecode reality that every constructor calls a super-constructor.

for a principal

Frames constructor design as API surface and invariants — when to require args vs. provide a no-arg ctor for tooling, and the maintenance cost of each choice across a codebase.

## What an object is In Java, a **class** is a blueprint and an **object** (or **instance**) is a concrete thing built from that blueprint. You build one with the `new` keyword: `Dog d = new Dog();`. Each object has **fields** (its data) and **methods** (its behavior). ## What a constructor is A **constructor** is a special piece of code that runs exactly once, automatically, at the moment an object is created with `new`. Its purpose is **initialization**: putting the new object into a valid starting state (assigning fields, validating arguments, allocating resources). A constructor differs from a normal method in three ways: 1. Its name is **identical to the class name** (`Dog` in class `Dog`). 2. It has **no return type** — not even `void`. 3. You don't call it like a method; the runtime calls it for you when you use `new`. ## The implicit (default) constructor If you write a class and declare **no constructor at all**, the code still compiles and `new Dog()` still works. That's because the Java **compiler** silently inserts a constructor for you. This is the **implicit default constructor** (often just "default constructor"). It is: - **no-argument** (takes nothing), - has an **empty body** (just one hidden line: `super();`, a call to the superclass's no-arg constructor), - and has the **same access modifier as the class** (a `public class` gets a `public` default constructor; a package-private class gets a package-private one). ```java class Dog { } // no constructor written // compiler treats it as if you wrote: class Dog { Dog() { super(); } // implicit default constructor } ``` ## The crucial condition The compiler generates this gift **only when you declare zero constructors**. If you write *any* constructor — even a parameterized one — the compiler assumes you know what you're doing and does **not** add the no-arg one. (More on that in the related question about when it disappears.) ## Why it matters Many frameworks and tools (JavaBeans, JPA/Hibernate entities, Jackson deserialization, reflection) need a public no-arg constructor to create objects. If you add a parameterized constructor and forget that the free no-arg one vanishes, those tools break — a very common real-world bug.

  • Does the default constructor initialize fields to anything?
    Its body is empty, but fields still get their default values (0, false, null) from Java's automatic field initialization, which happens regardless of the constructor.
  • Can a constructor have a return type if I add void?
    No. A method named like the class but with a return type (e.g. void Dog()) is a normal method, not a constructor, and will not run on new — a classic trap.

A constructor is like the setup wizard that runs the first time you open a new gadget. The default constructor is the bare 'just turn it on, no options' wizard the manufacturer ships when the designer didn't bother to write a custom one.

saying these in an interview costs you the question

  • Saying a constructor returns the object (it has no return type; new returns the reference)
  • Claiming the default constructor exists even after you write a parameterized one
  • Confusing the default constructor (compiler-generated) with default field values
  • Adding void before the constructor name, turning it into a regular method

context

open as a page

When does the Java compiler NOT generate the default no-arg constructor, and what problem can that cause?

level: middleimportance: must knowfreq 78%

basics

~20 s

As soon as you write any constructor of your own — even one that takes parameters — the compiler stops adding the free no-arg one. Then new MyClass() won't compile unless you also write the no-arg constructor yourself.

open as a page

What is constructor overloading in Java, and how does this() chaining work?

level: middleimportance: should knowfreq 70%

basics

~20 s

Constructor overloading means a class has several constructors with different parameter lists, so you can create objects in different ways. One constructor can call another in the same class using this(...) to avoid repeating setup code.

open as a page

When you call new on a class with a parameterized constructor, what runs and in what order (including super, field initializers, and the constructor body)?

level: seniorimportance: should knowfreq 55%

basics

~20 s

First the parent class is built (super runs), then this class's field initializers and instance initializer blocks run top to bottom, and finally the rest of your constructor body runs. So the parent is always fully set up before your constructor code executes.

open as a page

Constructors vs. static factory methods and the Builder pattern: when would you avoid exposing parameterized constructors directly?

level: principalimportance: nice to knowfreq 40%

basics

~20 s

Constructors are simple but limited: they must share the class name, can't return a cached or subclass object, and many similar parameters get confusing. Static factory methods and builders give named, flexible creation, so teams often prefer them for richer APIs.

open as a page