Properties, getters and controlled methods
Properties, getters and controlled methods
A report has a stored score and a pass status derived from that score. Storing both independently invites disagreement after a correction. A computed property calculates its value when accessed, while a stored property retains a value between operations.
What you’ll learn
- Derive a property instead of duplicating state.
- Restrict updates to controlled methods.
Properties, getters and controlled methods in the example
Stored score
A report has a stored score and a pass status derived from that score.
Stored scoreComputed pass decision
A property can store data or compute its result with a getter.
Computed pass decisionMethod returns summary
The initial computed value is false.
Method returns summary
Understand properties, getters and controlled methods
A property can store data or compute its result with a getter. A private setter permits external reads while directing changes through methods. Define operations that preserve the object’s contract rather than asking every caller to remember its internal rules.
Connect this to constructors and initialization checks and visibility and encapsulation.
Follow properties, getters and controlled methods
Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.
Kotlin/JVM · complete program
class Report(score: Int) {
var score = score
private set
val passed: Boolean get() = score >= 75
fun correct(newScore: Int) {
require(newScore in 0..100)
score = newScore
}
}
fun main() {
val report = Report(70)
println(report.passed)
report.correct(80)
println(report.passed)
}Expected output
false
trueFollow the execution
The initial computed value is false. Correct validates and stores 80, and the next getter returns true. A separate stored pass flag is unnecessary because the decision is derived from score.
Try this in the Kotlin compiler by pasting the complete program.
Practice: Properties, getters and controlled methods
Your task: Why compute passed rather than independently storing it?
Kotlin/JVM · complete program
class Report(score: Int) {
var score = score
private set
val passed: Boolean get() = score >= 75
fun correct(newScore: Int) {
require(newScore in 0..100)
score = newScore
}
}
fun main() {
val report = Report(70)
println(report.passed)
report.correct(80)
println(report.passed)
}Worked answer
It follows directly from the score and threshold, so a getter avoids a stale duplicate value after correction.
Properties, getters and controlled methods: the important boundary
This example’s constructor also needs range validation before real input use; the correction method alone does not protect initial state. Getters are not automatically cached, and an apparently simple property read should not hide surprising work.
Check your understanding
Properties, getters and controlled methods: what to keep in mind
It follows directly from the score and threshold, so a getter avoids a stale duplicate value after correction. This example’s constructor also needs range validation before real input use; the correction method alone does not protect initial state.
Further reading: official Kotlin documentation for this topic.
Reaching the recap and completing the quizzes are separate milestones.