Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when private is not so private

 
Tom Adams
Ranch Hand
Posts: 56
  • 0
  • 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.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • 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.
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • 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.
 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • 0
  • 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: 204
  • 0
  • 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