• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

when private is not so private  RSS feed

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one caught me...

Program result?
1. Compile Error (have you seen line 1?)
2. Runtime Exception
3. Output is 100

#3! Seems like accessing another instance's (of your class) private variables is ok.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. A private member is private to the class but not private to an object instance.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as the access expression to the private member (otherPrivate.i) occurs whithin the class (Private) declaring the private member (i), the access is allowed.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its is possible for the objects of the same class to access each others private methods because private member's is private to the class but not private to an object instance.
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction to the above post ...
Its is possible for the objects of the same class to access each others private methods/variables because private member is private to the class but not private to an object instance.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!