aspose file tools*
The moose likes Beginning Java and the fly likes Can objects share each other's private data ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can objects share each other Watch "Can objects share each other New topic
Author

Can objects share each other's private data ?

Ashish Vijay Joshi
Greenhorn

Joined: Nov 27, 2006
Posts: 10
I came across a question in this site only which says:

There are two objects of same class. A class has some private variables.
Can these two objects access these private variables of other.
EX:
class Demo
{
private int i;
}
in main()
{
Demo d1,d2;
}
now can d1 access i of d2 n vice-versa. If yes den how it could be done in coding.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

See if you can compile this code and discover the answer yourself...:

[ November 29, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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.

-- Kaydell
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
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.

-- Kaydell


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.


Joanne
Ashish Vijay Joshi
Greenhorn

Joined: Nov 27, 2006
Posts: 10
1.Static field case is ok because static fields are meant for sharing among all instances of a class.

2.In method case:
void method(Demo d)
{
System.out.println(d.i);
}
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.

Warm Regards
Ashish
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

Ashish, look again at the code example I posted.

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 ]
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Hi Ashish ,

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.



/Svend Rost
[ November 30, 2006: Message edited by: Svend Rost ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
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).


Have you tried compiling Jesper's code ?
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can objects share each other's private data ?