Back to questions
JuniorTheoryCommon
What does the program print?
object Counter {
var count = 0
}
fun main() {
val a = Counter
val b = Counter
a.count += 1
b.count += 1
println(Counter.count)
}2
- Expecting
1: thinkingval b = Countermakes a second object. Anobjecthas one instance, soaandbboth name it and share onecount. - Expecting
0: thinkingaandbare copies andCounterstays untouched. They are names for the object itself, so itscountis2.
- What does
println(a === b)print? - When is the single
Counterobject created?