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.
[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 ]
You can find plenty of examples in the JDK sources. Take a look at the source file String.java, 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 src.zip in your JDK folder.)
Oh, and welcome to the Ranch!
There are no new questions, but there may be new answers.
Joined: Dec 20, 2012
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...