Example code (Java):
public class MutableInteger {
private int value;
// Lots of stuff goes here
public boolean equals(Object o) {
if(!(o instanceof MutableInteger)){ return false; }
MutableInteger other = (MutableInteger) o;
return this.value == other.value; // <------------
}
}
If the assumption "private member variables are private to the instance" were correct, the marked line would cause a compiler error, because the other.value
field is private and part of a different object than the one whose equals()
method is being called.
But since in Java (and most other languages that have the visibility concept) private
visibility is per-class, access to the field is allowed to all code of the MutableInteger
, irrelevant of what instance was used to invoke it.