• Post Reply Bookmark Topic Watch Topic
  • New Topic

is there any API to access private members of a class?  RSS feed

 
Satya Siripuram
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any API to access private members of a class?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. The point of private members is that they can't be accessed from outside of the class.
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya Siripuram wrote:is there any API to access private members of a class?


Yes, it's called reflection, see the Class class. But it should be used in special cases only, if you need it often, your code is badly designed!
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya Siripuram wrote:is there any API to access private members of a class?


Yes, the Reflection API, but it's not recommended because you seriously compromise the type safety of your program.
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you'll find sample code.
 
Satya Siripuram
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much for your information.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the reflection workaround will not work in the presence of a security manager, and can easily lead to behavior that breaks class contracts - proceed very carefully. If something is private, then there's generally a reason for that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!