File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 Murach's Java Servlets and JSP this week in the Servlets 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaRanch question #208: private variable
 
Similar Threads
Can one object access the private variable of another object of the same class?
Java Rule Round-Up
Private Variables
Accessing private variables?
private instance variables