Kotlin
lateinit and lazy
Learn deferred initialization with lateinit and by lazy.
lateinit
lateinit works only for var properties of non-null reference types. It tells the compiler you will initialize the property later, not at declaration. This is handy when dependencies are wired during a setup phase.
Example: lateinit var service: UserService. Here service is not initialized at declaration; later service = RealService() assigns it. Reading it before assignment throws an UninitializedPropertyAccessException. It cannot be used with primitive types or val properties.
by lazy
by lazy computes a val property on first access and caches the result. This is the standard way to do lazy initialization; the computation runs only once.
Example: val config: String by lazy { loadConfig() }. On first read of config, loadConfig() runs and the result is stored. Subsequent reads return the same value. This is used to run expensive work only when needed. It is thread-safe by default.
Exercise Define the Config class
Define a class named Config. Add a lateinit var apiKey: String property. Add a val cachedConfig: String by lazy { "settings loaded" } property. In main create a Config instance, assign "secret123" to apiKey and print both properties with println.
Show example solution
class Config {
lateinit var apiKey: String
val cachedConfig: String by lazy {
"settings loaded"
}
}
fun main() {
val c = Config()
c.apiKey = "secret123"
println(c.apiKey)
println(c.cachedConfig)
}
Test yourself
-
Complete the lateinit definition
class App {
____ var service: UserService
}
Show answer
class App {
lateinit var service: UserService
}
-
Match lateinit and lazy concepts
- lateinit var
- Late-assigned var
- by lazy
- val computed on first access
- val by lazy
- Read-only lazy property
- UninitializedPropertyAccessException
- Early access error
-
Which is a valid by lazy definition?
- val config: String by lazy { load() }
- var config by lazy { load() }
- lateinit val config: String
- val config = lazy
Show answer
val config: String by lazy { load() } — by lazy is used with a val for a read-only property.
Next lesson: companion object