Back to questions
JuniorTheoryCommon
What does the program print?
class Counter {
var count = 0
val stored = count * 10
val computed get() = count * 10
}
fun main() {
val c = Counter()
c.count = 2
println("${c.stored} ${c.computed}")
}0 20
- Expecting
20 20: thinkingstoredchanges whencountdoes. Its initializer runs once, when the object is created, sostoredkeeps0. - Expecting
0 0: thinking a property withget()is computed once like any other.get()runs on every read, so it seescount = 2.
- What does
c.computedshow afterc.count = 5, and why? - Which of the two properties keeps its value inside the object, and which does not?