your question is not clear . if one or two objects are craeted for a class(say A) inside A, then the objects can acces private property . if a object lies outside the class(A), then private variable is not visible to the object.
I agree that the private method is being invoked on p2, or a reference of it. I could try to explain my reasoning for concluding one object access a private variable of another object of the same class. In the example I call invoke() from inside the Class Private (using a different object reference) I am able to access the same and there is no violation of any sort. But if I try to do the same from outside the Class definition the compiler would not permit. It can be demonstrated easily by adding
inside the main method. This would result in a compilation error:-
display() has private access in Private
I feel if I had added the above statement the relevance of the example would be better understood.
Joined: Sep 05, 2007
Not sure what exactly your question means. As you are aware one cannot access private variable of an object.
The situation like one you outilne occurs when you have a method in the class which takes a reference
to object of the same class as an argument.
See the below code snippet which overrides the "equals" method. Here the private variable
of the passed object is being referred.