There are two objects of same class. A class has some private variables.
Can these two objects access these private variables of other.
private int i;
now can d1 access i of d2 n vice-versa. If yes den how it could be done in coding.
Originally posted by Kaydell Leavitt:
I won't tell you the answer, but consider using a static field when two objects of the same class need to access each others data.
That won't be accessing each others data. That will be just sharing the same piece of data. If i was static, then each instance of the class would not be able to have a different value for it.
2.In method case:
void method(Demo d)
But even if d has a referencce for object demo d2 but inside a method
d.i is actually accessing the variable i which is of object d2 only.
So again the question remains how d1 access d2's i.
There are two Demo objects which I create in the main() method. The variables d1 and d2 refer to those Demo objects. Then I call method() on d1, and I pass it d2.
In the method, the object that d1 refers to is the current object. The variable d refers to the object that d2 in main() refers to. If this code compiles (did you try it yet?) then the answer is yes, one instance of class Demo can access the private variable of another instance of class Demo: the current object (d1 in main()) is accessing member variable i of the object that d refers to (which is the same object that d2 refers to in main()). That was what you're asking, isn't it?
Note that objects do not have names or identities. There is no "object d1" and "object d2" in the code. d1 and d2 are simply reference variables that point to two different Demo objects.
[ November 30, 2006: Message edited by: Jesper Young ]
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.
Can two objects indirectly get access to private variables of each other ?
Yes, via methods. You can defined get and set methods for the private
variables. Doing this means that you can control (more or less atleast) how
the variables are accessed.
[ November 30, 2006: Message edited by: Svend Rost ]
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...