This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain with an example

 
Suman Barick
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 825
5
Java Python Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same question has already been asked here and here.

Welcome to the Ranch!
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Suman Barick
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic