Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Justin Calleja
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic