🚀 Enrollments Open for Jetpack Compose Cohort 2 — 7 Days of Live Learning to Build Modern Android UIs 💚Join Now
KotlinIntermediate4 min
What is an Init block in Kotlin?

Answer

The `init` block is an initializer block in Kotlin classes.

Functionality

  • It runs immediately after the primary constructor.
  • It is used to run startup logic that requires the class properties to be initialized.
  • You can have multiple `init` blocks; they execute in the order they appear in the class body.
  • It is often used because the primary constructor cannot contain code (only property declarations).

Example

```kotlin class User(val name: String) { val upperName: String

init {
    println("Initializing User: $name")
    upperName = name.uppercase()
}

} ```

Want to master these concepts?

Join our live cohorts and build production-ready Android apps.

Accelerate Your Growth

Don't just learn concepts in isolation. Build production-ready Android apps with expert guidance.

Live Interactive Sessions
Code Reviews & Feedback
Real-world Projects
Career Guidance

Limited seats available for next cohort