skip to content

Secondary Constructors & this() Delegation

Secondary constructors exist for the cases the primary cannot express, and when a primary constructor is present they must delegate to it with this(...). Interviewers usually follow with 'why do you rarely need one in Kotlin' — default arguments are the answer.

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

questions

5

What is a secondary constructor in Kotlin, and how do you declare one?

level: juniorimportance: must knowfreq 60%

answer

  1. constructor keyword inside class body
  2. Has a body block; can run code
  3. Multiple allowed
  4. No val/var on its parameters
  5. Must delegate to primary if one exists

basics

~20 s

A secondary constructor is an extra way to create an object, written inside the class body with the constructor keyword. It lets you build an instance from a different set of arguments than the main one.

solid answer

~40 s

A secondary constructor is declared in the class body with the constructor keyword followed by a parameter list and a body block. A class may have several secondary constructors. They are useful when you need alternative ways to construct an object beyond the primary constructor's parameters, or when no primary constructor exists. Unlike the primary constructor, a secondary constructor can contain executable code in its body. If the class also has a primary constructor, every secondary constructor must delegate to it (directly or indirectly) via this(...). Parameters of a secondary constructor are plain parameters — you cannot put val/var there to declare properties; only the primary constructor can do that.

code

kotlin · 7 lines
kotlin
class User(val name: String, val age: Int) {
    // alternative entry point
    constructor(name: String) : this(name, age = 0)
}

val a = User("Ada", 36)
val b = User("Bo") // age defaults to 0

go deeper

for a junior

Can declare a secondary constructor with the constructor keyword and assign fields in its body.

for a middle

Knows multiple are allowed, that they have bodies, and that parameters can't be val/var.

for a senior

Frames secondary constructors against default params/factory functions and explains when each is idiomatic.

for a principal

Discusses API design trade-offs (binary compatibility, Java interop, framework constructor contracts) when choosing secondary constructors over factories.

## What a secondary constructor is A **secondary constructor** is a constructor declared **inside the class body** using the `constructor` keyword. It provides an alternative way to instantiate the class, with its own parameter list. ```kotlin class Point { val x: Int val y: Int // secondary constructor constructor(x: Int, y: Int) { this.x = x this.y = y } } ``` ## Key rules - The keyword is `constructor` (always required for secondary constructors; the `constructor` keyword on the **primary** constructor is optional and only needed for visibility/annotations). - A class can declare **multiple** secondary constructors. - A secondary constructor **has a body** (a `{ ... }` block) and can run arbitrary code — unlike the primary constructor, which has no body. - Secondary constructor parameters are **plain parameters**. You **cannot** write `val`/`var` in front of them to declare properties — only the **primary** constructor can declare properties inline. ## With vs. without a primary constructor If there is **no primary constructor**, a secondary constructor can fully initialize the object on its own (as in `Point` above). If there **is** a primary constructor, every secondary constructor must **delegate** to it using `this(...)`: ```kotlin class User(val name: String, val age: Int) { constructor(name: String) : this(name, age = 0) } ``` Here `constructor(name: String) : this(name, 0)` calls the primary constructor with a default age. ## Why use them - Provide alternative argument shapes (e.g., build from a raw `String` vs. parsed fields). - Run setup logic in the body. - Interoperate with Java frameworks that call specific constructor signatures. In idiomatic Kotlin, **default parameter values** and **factory functions** (`companion object` functions) often replace secondary constructors, but secondary constructors remain essential for inheritance/framework scenarios.

  • Can a secondary constructor declare a property with val/var in its parameter list?
    No. Only the primary constructor can declare properties inline. Secondary constructor parameters are ordinary parameters; you assign to declared properties in the body instead.
  • Does a secondary constructor need the constructor keyword?
    Yes, always. The keyword is optional only on the primary constructor (and there only when you need a visibility modifier or annotation).

The primary constructor is the front door; secondary constructors are side doors that ultimately lead into the same hallway.

saying these in an interview costs you the question

  • Claiming you can write val/var on secondary constructor parameters
  • Saying a class can have only one secondary constructor
  • Thinking the primary constructor has a body block like a secondary one does
  • Forgetting the constructor keyword is mandatory for secondary constructors

context

open as a page

When a class has a primary constructor, what rule governs secondary constructors, and how does this(...) delegation work?

level: middleimportance: must knowfreq 55%

basics

~10 s

If the class has a primary constructor, each secondary constructor must call it, directly or through another secondary constructor, using this(...). This guarantees the primary constructor always runs.

open as a page

In what order do the primary constructor's property initializers, init blocks, and a secondary constructor body execute?

level: middleimportance: should knowfreq 45%

basics

~10 s

First the primary constructor runs: property initializers and init blocks execute top to bottom. Then the secondary constructor's body runs afterward. So shared setup happens before the secondary-specific code.

open as a page

When should you prefer secondary constructors versus default parameter values or factory functions in Kotlin?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Prefer default parameter values for simple optional arguments because they need less code. Use secondary constructors mainly when you need different parameter types, extra setup logic, or to satisfy frameworks and Java that call specific constructors.

open as a page

How do secondary constructors behave when a class has no primary constructor, including delegation to a superclass?

level: seniorimportance: nice to knowfreq 30%

basics

~10 s

Without a primary constructor, a secondary constructor can fully initialize the object itself and may call the base class with super(...). There is no this(...) requirement because there is no primary to delegate to.

open as a page