| Author |
JavaRanch question #208: private variable
|
Gian Franco
blacksmith
Ranch Hand
Joined: Dec 16, 2003
Posts: 975
|
|
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: 975
|
|
Thanks Corey, I was looking at it with two objects instantiated outside the class, thereby forgetting the option you mentioned. Greetings, Gian Franco
|
 |
 |
|
|
subject: JavaRanch question #208: private variable
|
|
|