Originally posted by Svend Rost:
Can two objects access (directly) the private variables of each other ?
No, it is not possible to get access to an objects private members (i.e. variables or methods). After all, when we use the private keyword it is because we wish to "hide" the members of the object.
It is not possible for two objects to access (directly) the private variables of each other.
But, there is an exception to this rule! If the code is within the same class as that of the objects, then one object CAN access the private members of other object directly. For example, see this code:-
In the above code, the first compilation
unit (AccessRules.java) will compile correctly, but, the second one will produce errors. The errors show up because we directly try to access an object's private members & the code does not reside inside the same class.
Hope this helps...
Best regards,
Abdul Rehman.