assignment of two Scala class instances
Solution 1:
Scala variables are just pointers, so this:
counter1 = counter2
makes both counter1
and counter2
point to the same instance.
This kind of hidden dependency is why you should avoid var
as much as possible and always create new objects rather than modifying existing objects.
Solution 2:
Yes. Just like in Java. This you did is a shallow copy. If you want to assign the value of one be copied to the other you need a deep copy. You need counter1.counter = counter2.counter