0Pricing
Kotlin Academy · Lesson

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

  1. Primary Constructor and Property Parameters
  2. init Blocks and Initialization Order
  3. Custom Getters and Setters with field
  4. lateinit and Lazy Initialization
← Back to Kotlin Academy