100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Constructors in Kotlin (Primary and Secondary)

Understand how primary and secondary constructors initialize Kotlin objects, including init blocks and constructor delegation.

Classes & ObjectsBeginner9 min readJul 8, 2026
Analogies

Introduction

Constructors initialize a new instance of a class. Kotlin supports one primary constructor, declared in the class header, and any number of secondary constructors, declared inside the class body with the constructor keyword. Secondary constructors offer alternate ways to create an object but must ultimately delegate to the primary constructor if one exists.

🏏

Cricket analogy: A cricketer's central contract is the primary constructor set once by the board; a temporary IPL loan deal is a secondary constructor, an alternate signing path that must still route back through the central board contract.

Syntax

kotlin
class Person(val name: String) {
    var age: Int = 0

    init {
        println("Person created: $name")
    }

    constructor(name: String, age: Int) : this(name) {
        this.age = age
    }
}

Explanation

The primary constructor (val name: String) sits in the class header and cannot contain code directly; instead, initialization logic goes into one or more init blocks, which run in the order they appear, interleaved with property initializers, as part of primary constructor execution. The secondary constructor uses constructor(...) and delegates to the primary constructor via : this(name). This delegation is mandatory whenever a primary constructor exists, ensuring the primary constructor's initialization always runs first.

🏏

Cricket analogy: The primary constructor (val name: String) is like a player's core registration form with no extra notes allowed directly, so fitness-check init blocks run in order right after; a secondary loan-deal constructor must call : this(name), always processing the core registration first.

Example

kotlin
class Person(val name: String) {
    var age: Int = 0

    init {
        println("Person created: $name")
    }

    constructor(name: String, age: Int) : this(name) {
        this.age = age
        println("Age set to $age")
    }
}

fun main() {
    val p1 = Person("Bob")
    val p2 = Person("Carol", 25)
}

Output

text
Person created: Bob
Person created: Carol
Age set to 25

Key Takeaways

  • A class can have exactly one primary constructor, declared in the class header.
  • init blocks contain initialization logic and run in declaration order as part of the primary constructor.
  • Secondary constructors are declared with the constructor keyword inside the class body.
  • A secondary constructor must delegate to the primary constructor using this(...) when one exists.
  • Multiple init blocks and property initializers execute top-to-bottom in the order written.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#ConstructorsInKotlinPrimaryAndSecondary#Constructors#Primary#Secondary#Syntax#StudyNotes#SkillVeris