skip to content

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