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

Accessing Private members  RSS feed

 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know, if the private or default methods in a public class can be accessed from outside the package in which the class is present. I have tried and found an indirect way of doing it, by calling another method in that class. Is there any other direct method by which this can be done?
Thanks.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes, you can use setAccessible on java.lang.reflect.AccessibleObject...
[ February 22, 2006: Message edited by: Jeff Albertson ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole point of access control is to... control access. Meaning that no, there's no direct way. If a member X is private, a class can allow limited access by including getX() and/or setX() methods. Otherwise, you have to cheat using reflection and "setAccessible(true)."
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!