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
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
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
Person created: Bob
Person created: Carol
Age set to 25Key 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
1. Where does initialization code for a Kotlin primary constructor go?
2. How is a secondary constructor declared?
3. If a class has a primary constructor, what must a secondary constructor do?
4. In what order do init blocks and property initializers run?
Was this page helpful?
You May Also Like
Classes in Kotlin
Learn how Kotlin classes bundle state and behavior with concise constructor syntax and final-by-default semantics.
Data Classes in Kotlin
See how Kotlin data classes auto-generate equals, hashCode, toString, copy, and componentN functions for value-holding objects.
Default and Named Arguments in Kotlin
Simplify function calls in Kotlin using default parameter values and named arguments instead of overloads.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics