aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JavaRanch question #208: private variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JavaRanch question #208: private variable" Watch "JavaRanch question #208: private variable" New topic
Author

JavaRanch question #208: private variable

Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi,

JRR question #208: 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.

How should the objects do that: access each other's private
data? I must be forgetting something.
Greetings,
Gian Franco


"Eppur si muove!"
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Here's an example for you:

You can see, in line 1, we're directly accessing the private member of another object. This can be done because this code is executing within the definition of that class. Outside this class, it can't be done.
I hope that helps,
Corey


SCJP Tipline, etc.
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Thanks Corey,
I was looking at it with two objects instantiated
outside the class, thereby forgetting the option
you mentioned.
Greetings,
Gian Franco
 
wood burning stoves
 
subject: JavaRanch question #208: private variable