IntermediateAdvanced

lazy Delegate

Initialize a property only when first accessed.

kotlin
fun main() {
    val message: String by lazy {
        "Loaded only when used"
    }
    println("Before using message")
    println(message)
}
Output
Before using message Loaded only when used

Explanation

by lazy defers the computation until the property is first accessed. The lambda runs once and the result is cached.