• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Can one object access a private variable of another object of the same class?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is from the JavaRanch Rule Round-up Game:

Can one object access a private variable of another object of the same class?

Answer: Yes
Private means "private to the class", NOT "private to the object". So two objects of the same class could access each other's private data.

Can anyone make this clearer please? How do two objects of the same class access each other's private data? I'm not exactly sure what it's referring to.

Here's something to work with (I think):



Thanks
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Justin,

Java's private access modifier means only private for a class, as you correctly pointed out. This ways any instance of this class can access the private members of another instance of this class like they were public. That means you can directly reference the fields or methods of a Car instance if you are inside another instance of Car. This is often used in implementations of the equals() method for example.

This example is not the best but it shows that you can directly access private members of the other Car instance

Marco
 
Justin Calleja
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marco,

Java's private access modifier means only private for a class, as you correctly pointed out.



lol, I just quoted the answer.

ahh that's what it was referring to.

Still, I think this sentence is confusing:

So two objects of the same class could access each other's private data.



I kept thinking:

Well lets say I have another class X which has a method:

public int timesWrecked(Car c) {

c.getSomething(); //where something is private

}

Now if I have an instance of X, and I do x.timesWrecked(c); there you go, I just accessed private data from an object which is not of the same class... use a getter... whatever.

Anyway, yes the equals() one made sense. You wouldn't be able to do otherCar.gear in a class other than Car.

Thanks, that cleared things up (wasn't sure what the question was referring to)

Regards,

Justin
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

I guess this concept of "private" access is confusing for a lot of people who are new to Java. At least it is not obvious at first sight that members can be accessed from different instances even though they are private.

On the other hand it would be very difficult to impossible to implement something like the said equals() methods. Without the private access modifier working the way it does you'd have to expose more internal data than necessary via getters or other methods which in general is surely not what you want.

Anyway,! I'm glad that my answer helped to clear your doubts.

Marco
 
ice is for people that are not already cool. Chill with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic