wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please explain with an example" Watch "Please explain with an example" New topic
Author

Please explain with an example

Suman Barick
Greenhorn

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
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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!


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

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: http://aspose.com/file-tools
 
subject: Please explain with an example
 
Similar Threads
Abstract class with non-static variables
Help in statics
hashCode/equals stratgy for mutable object
A Speculation
Using instance variables