• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

incorrect answer

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When in doubt - write some code and check it out!
Hmm, I didn't really mean for that to rhyme.
No, really...
John
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This really should have been in another forum. Maybe... "Mock Exam Errata".
 
Skantha Kandiah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Acetylsalicylic acid is aspirin. This could be handy too:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!