init Blocks and Initialization Order
Use init blocks and understand the order of property and block initialization.
What Is an init Block?
An init { } block contains code that runs when an instance is constructed. It is part of the primary constructor logic.
Simple init Block
The init block runs during construction, after property initializers in source order.
class Logger(val name: String) {
init {
println("Constructed Logger("$name")")
}
}
fun main() {
val l = Logger("auth")
}All lessons in this course
- Primary Constructor and Property Parameters
- init Blocks and Initialization Order
- Custom Getters and Setters with field
- lateinit and Lazy Initialization