0
0
Kotlinprogramming~10 mins

Secondary constructors in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Secondary constructors
Class Declaration
Primary Constructor?
YesInitialize Primary
Secondary Constructor
Call Primary or Another Secondary
Initialize Properties
Object Created
Shows how Kotlin class creation flows through primary and secondary constructors, with secondary constructors calling primary or other secondary ones before initializing.
Execution Sample
Kotlin
class Person(val name: String) {
    var age: Int = 0
    constructor(name: String, age: Int) : this(name) {
        this.age = age
    }
}

val p = Person("Anna", 25)
Creates a Person object using a secondary constructor that sets name and age.
Execution Table
StepActionConstructor CalledProperty ValuesNotes
1Create Person("Anna", 25)Secondary constructor (name, age)name=uninitialized, age=0Start secondary constructor
2Call primary constructor with name="Anna"Primary constructor (name)name="Anna", age=0Primary constructor initializes name
3Execute secondary constructor bodySecondary constructor (name, age)name="Anna", age=25Secondary constructor sets age
4Object creation completePerson instancename="Anna", age=25Person object ready
💡 Secondary constructor calls primary constructor first, then sets additional properties, finishing object creation.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
nameuninitializeduninitialized"Anna""Anna""Anna"
age0002525
Key Moments - 3 Insights
Why does the secondary constructor call the primary constructor first?
Because in Kotlin, every secondary constructor must delegate to the primary constructor (directly or indirectly) to ensure the base properties are initialized properly, as shown in execution_table step 2.
What happens if the secondary constructor does not call the primary constructor?
The code will not compile. Kotlin requires secondary constructors to delegate to the primary constructor to maintain consistent initialization, as seen in the flow and step 2 of the execution_table.
When is the property 'age' set in the example?
The 'age' property is set in the body of the secondary constructor after the primary constructor has initialized 'name', as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'name' after step 2?
A"Anna"
Buninitialized
Cnull
D25
💡 Hint
Check the 'Property Values' column in execution_table row for step 2.
At which step does the secondary constructor set the 'age' property?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Notes' column in execution_table for when 'age' changes.
If the secondary constructor did not call the primary constructor, what would happen?
AThe object is created without 'name'
BCompilation error occurs
CThe program runs normally
DThe secondary constructor is ignored
💡 Hint
Refer to key_moments about constructor delegation requirement.
Concept Snapshot
Secondary constructors in Kotlin allow multiple ways to create objects.
Each secondary constructor must call the primary constructor.
Primary constructor initializes main properties.
Secondary constructors add or override initialization.
This ensures consistent object setup.
Full Transcript
In Kotlin, a class can have a primary constructor and one or more secondary constructors. When you create an object using a secondary constructor, Kotlin requires that this constructor calls the primary constructor first. This ensures that the main properties defined in the primary constructor are initialized properly. After calling the primary constructor, the secondary constructor can set additional properties or perform extra setup. For example, in the Person class, the secondary constructor takes both name and age. It calls the primary constructor with name, then sets the age property. This flow guarantees that the object is fully and correctly initialized before it is used.