Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt from ranch roundup

 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

thanx
 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, you didn't make age private in Fish. Does it still work if age is private?
 
vjy chin
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it still works if age is private in Fish.

Look at the example
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic