aspose file tools*
The moose likes Beginning Java and the fly likes Can one object access a private variable of another object of the same class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can one object access a private variable of another object of the same class?" Watch "Can one object access a private variable of another object of the same class?" New topic
Author

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

Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
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

Joined: Mar 07, 2007
Posts: 1278

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

Joined: Sep 18, 2010
Posts: 18
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

Joined: Mar 07, 2007
Posts: 1278

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can one object access a private variable of another object of the same class?
 
Similar Threads
JavaRanch question #208: private variable
Java Rule Round-Up
hashCode and equals implementation
Using final
Question from http://www.javaranch.com/game.html...