aspose file tools
The moose likes Ranch Office and the fly likes incorrect answer Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » This Site » Ranch Office
Reply Bookmark "incorrect answer" Watch "incorrect answer" New topic
Author

incorrect answer

Skantha Kandiah
Greenhorn

Joined: Jan 19, 2001
Posts: 4
In the java rule round-up game the answer to question #208:
"Can an object access a private variable of another object of the same class?"
is set to "Yes". How is that possible? Seems to me that the answer should be "No".
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9001
Originally posted by Skantha Kandiah:
In the java rule round-up game the answer to question #208:
"Can an object access a private variable of another object of the same class?"
is set to "Yes". How is that possible? Seems to me that the answer should be "No".

The clue is in the words "same class". Private means that the variable is private to that class.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
gautham kasinath
Ranch Hand

Joined: Dec 01, 2000
Posts: 583
Hi! Skhantha,
Well U missed the "same class" like pointed out... but u r a little right the object1 cannot get the value or change that of the private variable of object2 even if they are objects of the same class.
Regds
Gautham Kasinath


"In the country of the blind, the one eyed man is the King"
Gautham Kasinath CV at : http://www.geocities.com/gkasinath
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Actually they can. Private variables are private to the class not the object. If object1 and object2 are both objects of the same class, then object1 can change the private variables of object2.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1441
When in doubt - write some code and check it out!
Hmm, I didn't really mean for that to rhyme.
No, really...
John


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
This really should have been in another forum. Maybe... "Mock Exam Errata".
Skantha Kandiah
Greenhorn

Joined: Jan 19, 2001
Posts: 4
I wrote a simple program in Java to check this. Looks like the private access specifier applies to the class and not to the object... so two objects instantiated from the same class can access and modify each others private variables. Amazing!
Next time I have a similar question I will post it on the other Forum.
Thanks,
Skantha
 
 
subject: incorrect answer
 
Threads others viewed
ResultSet
what is the output
Notify Method
implements Runnable
Getting 1Demsion Array from a 2Demsion Array
developer file tools