File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt from ranch roundup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt from ranch roundup" Watch "doubt from ranch roundup" New topic

doubt from ranch roundup

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 812
hi in question from roundup

Q can one object access a pvt variable of another object of the same class ?

A. yes, pvt means "pvt to the class" not "pvt to the object". so two objects of the same class could access each other's pvt data.

i didn't understand the question well ?
pls give one eg.


Thanks and Regards, Amit Taneja
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 528

If two objects are from the 'same class' then those objects can access each others private members.
When you think private, think class level, not object level.

Hope this helps.

Cheers, Marzo.


Live life to an interface, not an implementation!
Philip Heller
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
Here's an example:

See how the method accesses not only the age of the current Fish instance, but also the age of another instance? Perfectly legal.

Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
However, you didn't make age private in Fish. Does it still work if age is private?
vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
Yes, it still works if age is private in Fish.

Look at the example
I agree. Here's the link:
subject: doubt from ranch roundup
jQuery in Action, 3rd edition