🐦 Kotlin
Beginner
What is the init block in Kotlin?
Answer
The init block contains initialization code that runs as part of the primary constructor when an object is created. It executes in the order it appears in the class body, interleaved with property initializers. Multiple init blocks are allowed. Example: init { require(age >= 0) { "Age cannot be negative" } }. This is the place for validation logic and setup that cannot be expressed as a single property initializer expression. Secondary constructors must delegate to the primary constructor, so init blocks always run regardless of which constructor is used.