File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Please explain with an example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please explain with an example" Watch "Please explain with an example" New topic

Please explain with an example

Suman Barick

Joined: Dec 20, 2012
Posts: 2
I was playing the Rule-Roundup game. There I found a question like "Can one object access a private variable of another object of the same class?"... The answer was given as "Yes. Because the private variables are private to the class and NOT to the object"...
I am unable to understand this, because, as per my understanding of class and object, I am an object instance of, say, "Human" class, and You are also an object made from that same "Human" class, and Human class has a private variable, say, private String bloodGroup ... But, how would I know (access) your blood group if you do not tell me?
Please, clear my doubt with some code example.
Thank you.

[Sorry, I could not find which section of this vast forum will be applicable for my question, so I put it here since I am a beginner and have started reading Head First Java ]
Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

The same question has already been asked here and here.

Welcome to the Ranch!

The quieter you are, the more you are able to hear.
Darryl Burke

Joined: May 03, 2008
Posts: 5115

You can find plenty of examples in the JDK sources. Take a look at the source file, and go the the equals(...) method. See the three fields used in determining the equality of the other String? Find the declarations of those fields. Are any/all of them private?

(In case you're not aware of it, you can find the sources in in your JDK folder.)

Oh, and welcome to the Ranch!

luck, db
There are no new questions, but there may be new answers.
Suman Barick

Joined: Dec 20, 2012
Posts: 2
Thank you Kemal.
Yes, I found those two links after posting it here and googling the question. But, still, I let this post be here because I thought, may be, some new examples, clearer and easier might come out.
As the signature of Darryl goes "There are no new questions, but there may be new answers".

All the examples/pointers are, in some way or other, related to "equals" method. Now, I am going to try my hands on Directly fetching/retriving value from that private variable, instead of equaling (==) it...Thank you Darryl, I really was not aware of that. I will definitely explore it.

But, anyway, I got it... Thanks to both of you again for your reply.

[not applicable to this discussion though, I am deeply moved by the thought conveyed by both of your signature lines. Really, worth thinking again and again...]

So, I will be quieter now, to hear (realize) more, both from inside and outside...
I agree. Here's the link:
subject: Please explain with an example
It's not a secret anymore!