0
0
Kotlinprogramming~10 mins

Primary constructor and init blocks in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Primary constructor and init blocks
Class Declaration with Primary Constructor
Object Creation
Primary Constructor Parameters Assigned
Init Blocks Run
Object Ready
When creating an object, Kotlin assigns primary constructor parameters to properties first, then runs init blocks in order, preparing the object.
Execution Sample
Kotlin
class Person(val name: String) {
    init {
        println("Init block: Name is $name")
    }
}

fun main() {
    val p = Person("Alice")
}
This code creates a Person object with a primary constructor and an init block that prints the name.
Execution Table
StepActionEvaluationOutput
1Call Person("Alice")Primary constructor parameter name = "Alice"
2Enter init blockAccess name = "Alice"Init block: Name is Alice
3Finish init blockNo further action
4Object creation completePerson object with name="Alice" ready
💡 Init block runs after primary constructor parameters are set; object is ready after init blocks finish.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
nameundefined"Alice""Alice""Alice"
Person objectnonecreatinginit block runningcreated
Key Moments - 2 Insights
Why can the init block access the primary constructor parameter 'name'?
Because the primary constructor parameters are available as properties during init block execution, as shown in step 2 of the execution_table.
Does the init block run before or after the primary constructor parameters are assigned?
The parameters are assigned first, then init blocks run, so init blocks can use those parameters (see step 1 and 2 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'name' during the init block (step 2)?
A"Alice"
Bundefined
Cnull
D"Bob"
💡 Hint
Check the 'Evaluation' column at step 2 in execution_table.
At which step does the init block finish running?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column describing init block progress in execution_table.
If we add another init block after the first, when will it run?
ABefore primary constructor parameters are assigned
BBefore the first init block
CAfter the first init block
DNever
💡 Hint
Init blocks run in the order they appear, after parameters are assigned (see concept_flow).
Concept Snapshot
class ClassName(val param: Type) {
  init {
    // code runs after primary constructor parameters are set
  }
}

- Primary constructor parameters are available in init blocks.
- Init blocks run in order during object creation.
- Useful for initialization logic needing constructor data.
Full Transcript
In Kotlin, when you create a class with a primary constructor, the parameters are set first. Then, any init blocks run in the order they appear. These init blocks can use the constructor parameters to run code during object creation. For example, if you have a class Person with a parameter name, the init block can print the name. The object is fully ready after all init blocks finish running.