What happens when an object is assigned to another object

What I understand is the system will just copy the values of e1 instance into e2 instance.

No, when you do e2 = e1 you’re copying object references – you’re not making a copy of the object – and so the variables e1 and e2 will both point to the same object.

And so when you do your increments, they’re all incrementing the same count field.

It’s only without the assignment e2 = e1 that the increments are happening on two different instances.

Leave a Comment