0
0
Kotlinprogramming~10 mins

Custom getters and setters in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom getters and setters
Property declared
Getter called?
NoReturn stored value
Yes
Execute custom getter code
Setter called?
NoNo change
Yes
Execute custom setter code
Update stored value if setter allows
End
When you access a property, Kotlin runs the getter code. When you assign a value, Kotlin runs the setter code. Custom code can control how values are read or changed.
Execution Sample
Kotlin
var age: Int = 0
    get() = field
    set(value) {
        if (value >= 0) field = value
    }
Defines an age property with a custom setter that only accepts non-negative values.
Execution Table
StepActionInput/ValueGetter Called?Setter Called?Stored Value (field)Output/Result
1Initialize age0NoNo00
2Set age to 1010NoYes1010
3Get ageN/AYesNo1010
4Set age to -5-5NoYes10Setter ignores negative, value unchanged
5Get ageN/AYesNo1010
6Set age to 2020NoYes2020
7Get ageN/AYesNo2020
💡 Execution ends after last get; setter ignores invalid negative value, so stored value stays valid.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
age (field)00101010102020
Key Moments - 2 Insights
Why does the stored value not change when setting age to -5?
Because the custom setter checks if the value is negative and ignores it if so, as shown in step 4 of the execution_table where setter is called but stored value remains 10.
When is the getter code executed?
The getter runs whenever the property is read, like in steps 3, 5, and 7 where 'Getter Called?' is Yes and the stored value is returned.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the stored value of age after step 4?
A0
B10
C-5
D20
💡 Hint
Check the 'Stored Value (field)' column at step 4 where setter ignores negative value.
At which step does the getter first return the value 10?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at 'Getter Called?' and 'Output/Result' columns in execution_table.
If the setter did not check for negative values, what would be the stored value after step 4?
A0
B10
C-5
D20
💡 Hint
Consider what happens if setter always assigns the input value without condition.
Concept Snapshot
Custom getters and setters in Kotlin let you run code when reading or writing a property.
Use 'get()' to customize reading, 'set(value)' to customize writing.
Inside setter, use 'field' to access the actual stored value.
You can add checks or transformations before changing the value.
This helps keep data valid and control property behavior.
Full Transcript
This example shows a Kotlin property 'age' with custom getter and setter. The getter simply returns the stored value. The setter only updates the stored value if the new value is zero or positive. The execution table traces initializing age to 0, setting it to 10, reading it, trying to set it to -5 (which is ignored), reading again, setting to 20, and reading again. The variable tracker shows how the stored value changes only when valid values are set. Key moments clarify why negative values are ignored and when the getter runs. The quiz tests understanding of these steps. This helps beginners see how custom getters and setters control property access and assignment in Kotlin.