• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can objects share each other's private data ?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See if you can compile this code and discover the answer yourself...:

[ November 29, 2006: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ashish Vijay Joshi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic