0
0
Kotlinprogramming~10 mins

Lazy property delegation in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lazy property delegation
Declare lazy property
Access property first time?
NoReturn cached value
Yes
Run initializer lambda
Store result
Return stored value
When you declare a lazy property, its value is not created until you use it the first time. Then it saves the value and returns it on later uses.
Execution Sample
Kotlin
val lazyValue by lazy {
    println("Computing value...")
    "Hello"
}

println(lazyValue)
println(lazyValue)
This code creates a lazy property that prints a message and returns "Hello" only when accessed the first time, then reuses the value.
Execution Table
StepActionLazy Property StateOutput
1Declare lazyValue with initializerNot initialized
2Access lazyValue first timeNot initialized
3Run initializer lambdaInitializingComputing value...
4Store result "Hello"Initialized with "Hello"
5Return stored valueInitialized with "Hello"Hello
6Access lazyValue second timeInitialized with "Hello"
7Return stored value without running initializerInitialized with "Hello"Hello
💡 After first access, lazyValue is initialized and reused without running initializer again.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 7
lazyValueNot initializedInitializingInitialized with "Hello"Initialized with "Hello"
Key Moments - 2 Insights
Why does "Computing value..." print only once even though we access lazyValue twice?
Because at step 3 the initializer runs only once and stores the result at step 4. Later accesses (step 7) reuse the stored value without running the initializer again.
What happens if we never access lazyValue?
The initializer lambda never runs, so "Computing value..." is never printed and the value is never created, as shown by the state staying "Not initialized" in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of lazyValue after step 4?
AInitialized with "Hello"
BInitializing
CNot initialized
DUninitialized but accessed
💡 Hint
Check the 'Lazy Property State' column at step 4 in the execution_table.
At which step does the initializer lambda run?
AStep 2
BStep 3
CStep 5
DStep 7
💡 Hint
Look for the step where 'Run initializer lambda' happens in the Action column.
If we never accessed lazyValue, what would be printed?
A"Computing value..." once
B"Hello" once
CNothing
D"Computing value..." twice
💡 Hint
Refer to the key_moments about what happens if lazyValue is never accessed.
Concept Snapshot
Lazy property delegation in Kotlin:
- Declare with 'val x by lazy { initializer }'
- Initializer runs only on first access
- Result is stored and reused
- Saves resources by delaying computation
- Useful for expensive or optional values
Full Transcript
Lazy property delegation means the value is created only when you first use it. The code declares a lazy property with an initializer lambda. When you access the property the first time, the lambda runs, prints a message, and returns a value. This value is saved. On later accesses, the saved value is returned directly without running the lambda again. If you never access the property, the initializer never runs. This helps save work until needed.