I came across this question in Java Ranch, "Can one object access a private variable of another object of the same class?". The site says this is possible. Can someone explain or justify this statement with a sample code? Thank you - Preetha
My guess for the reason that Java allow an objectcan access private instant members of other objects of the same class is:
Equals() is one of the most essential method in Java. It requests that the object whose euqals() method is invoked can compares its attributes with the object which is compared to. The attributes to compare should not be restricted to exclude those with private access modifier, which is totally agains the common sense. Therefore, an object can access private instant members of other objects in the same class.