0
0
KotlinComparisonBeginner · 4 min read

Primary vs Secondary Constructor in Kotlin: Key Differences and Usage

In Kotlin, a primary constructor is a concise way to initialize a class with parameters directly in its header, while secondary constructors are additional constructors defined inside the class body for alternative initialization. The primary constructor is preferred for simple initialization, and secondary constructors provide flexibility for multiple ways to create an object.
⚖️

Quick Comparison

This table summarizes the main differences between primary and secondary constructors in Kotlin.

AspectPrimary ConstructorSecondary Constructor
DefinitionDeclared in class headerDeclared inside class body with constructor keyword
Number per classAt most oneMultiple allowed
InitializationDirectly initializes propertiesCan delegate to primary constructor or other secondary constructors
UsageFor main initialization logicFor alternative ways to create instances
Syntax simplicityMore concise and clearMore verbose, used for special cases
Access to init blocksAutomatically calls init blocksMust delegate to primary constructor to call init blocks
⚖️

Key Differences

The primary constructor is part of the class header and is the main way to pass parameters to a class. It allows you to declare and initialize properties directly, making the code concise and easy to read. When you create an instance, the primary constructor runs first, followed by any init blocks for additional setup.

Secondary constructors are defined inside the class body using the constructor keyword. They provide alternative ways to create an object with different parameters or initialization logic. Each secondary constructor must either delegate to the primary constructor or another secondary constructor to ensure the class is properly initialized.

While the primary constructor is limited to one per class, you can have multiple secondary constructors to handle various initialization scenarios. Secondary constructors are useful when you need to support different sets of parameters or complex initialization that doesn't fit well in the primary constructor.

⚖️

Code Comparison

Here is an example showing a class with a primary constructor that initializes properties directly.

kotlin
class Person(val name: String, val age: Int) {
    init {
        println("Person created: $name, age $age")
    }
}

fun main() {
    val person = Person("Alice", 30)
}
Output
Person created: Alice, age 30
↔️

Secondary Constructor Equivalent

The same class using a secondary constructor inside the class body. It does not delegate to the primary constructor but initializes properties directly.

kotlin
class Person {
    val name: String
    val age: Int

    constructor(name: String, age: Int) {
        this.name = name
        this.age = age
        println("Person created: $name, age $age")
    }
}

fun main() {
    val person = Person("Alice", 30)
}
Output
Person created: Alice, age 30
🎯

When to Use Which

Choose a primary constructor when your class has a straightforward initialization with a fixed set of parameters. It keeps your code clean and concise, and works well with Kotlin features like data class.

Choose secondary constructors when you need multiple ways to create an object, such as optional parameters or different initialization logic. They are helpful for backward compatibility or complex setups that don't fit neatly into the primary constructor.

Key Takeaways

Primary constructors provide a simple, concise way to initialize classes with one main parameter list.
Secondary constructors allow multiple ways to create an object with different parameters or logic.
Every secondary constructor must delegate to the primary constructor or another secondary constructor.
Use primary constructors for most cases and secondary constructors for alternative initialization needs.
Primary constructors automatically call init blocks; secondary constructors do so only if they delegate properly.