28 Class identity
Class variables can refer to the same entity or to distinct entities with equal-looking fields.
norm
class Counter {
Integer value
}
main() {
Counter first = Counter(value: 2)
Counter alias = first
Counter equalFields = Counter(value: 3)
alias.value = 3
printLine(first.value)
printLine(first == alias)
printLine(first == equalFields)
}Expected output:
text
3
true
falsealias shares the exact object with first, so writing through it changes first.value to three. A separately constructed object also holds three but has a distinct identity.
Try it: Assign equalFields = first and predict the last comparison.
Precise rules: Reference.