init Blocks and Initialization Order
Use init blocks and understand the order of property and block initialization.
init Blocks and Initialization Order is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
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")
}Validation in init
Use require / check inside init to enforce invariants at construction time.
class Percentage(val value: Int) {
init {
require(value in 0..100) { "value must be 0..100, was $value" }
}
}
fun main() {
println(Percentage(75).value)
// Percentage(150) // throws IllegalArgumentException
}Multiple init Blocks
You can have several init blocks; they execute in source order, interleaved with property initializers.
class Demo(val x: Int) {
init { println("First init, x=$x") }
val doubled = x * 2
init { println("Second init, doubled=$doubled") }
}
fun main() {
Demo(5)
}Property Initializers and init Order
Property initializers and init blocks run top-to-bottom in source order. A property must be assigned before it is used.
class Order {
val createdAt = System.currentTimeMillis()
init { println("created at $createdAt") }
val nextId = 1
init { println("next id = $nextId") }
}
fun main() {
Order()
}Cannot Use Properties Before They Initialize
Referencing a property before its initializer runs is a compile error.
class Bad {
init {
// println(name) // ERROR: name not yet initialized
}
val name = "after init"
}
fun main() { Bad() }Computing Derived Properties
Use init blocks to compute derived state that depends on multiple constructor parameters.
class Rectangle(val width: Int, val height: Int) {
val area: Int
init {
area = width * height
}
}
fun main() {
val r = Rectangle(4, 5)
println("area = ${r.area}") // 20
}Side Effects: Logging
Use init for non-trivial setup like logging, allocation, or registration.
class Service(val name: String) {
init {
println("[BOOT] starting service '$name'")
}
}
fun main() {
Service("auth")
Service("payments")
}Init Order with Subclasses
Superclass init runs before subclass init. Each class's own properties initialize in source order.
open class Base {
init { println("Base init") }
}
class Sub : Base() {
init { println("Sub init") }
}
fun main() { Sub() }Avoid Heavy Logic in init
Long-running init blocks slow down every construction. Consider lazy initialization or factory methods if setup is expensive.
class Heavy(val n: Int) {
val data: List<Int> by lazy { (1..n).toList() }
}
fun main() {
val h = Heavy(1_000_000)
println("Created without computing data")
println("First element: ${h.data[0]}") // now data initializes
}Combining init with Secondary Constructors
Secondary constructors delegate to the primary constructor; init blocks run as part of the primary path.
class Box(val content: String) {
init { println("init for '$content'") }
constructor(): this("empty") { println("secondary done") }
}
fun main() {
Box()
}Quick Check
What is the execution order of multiple init blocks and property initializers in a Kotlin class?
Recap
init blocks contain construction-time logic — validation, derived properties, side effects. Multiple blocks execute in source order, interleaved with property initializers. Avoid heavy work in init; use lazy or factory methods instead.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “init Blocks and Initialization Order” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “init Blocks and Initialization Order”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
What will I learn in “init Blocks and Initialization Order”?
Use init blocks and understand the order of property and block initialization. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “init Blocks and Initialization Order” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
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