Author
is there any API to access private members of a class?
Satya Siripuram
Greenhorn
Joined: Nov 15, 2007
Posts: 2
is there any API to access private members of a class?
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
posted Nov 23, 2009 01:42:14
0
No. The point of private members is that they can't be accessed from outside of the class.
Android apps – ImageJ plugins – Java web charts
Lorand Komaromi
Ranch Hand
Joined: Oct 08, 2009
Posts: 276
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!
OCJP 6 (93%)
Embla Tingeling
Ranch Hand
Joined: Oct 22, 2009
Posts: 237
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
Joined: Oct 08, 2009
Posts: 276
Here you'll find sample code.
Satya Siripuram
Greenhorn
Joined: Nov 15, 2007
Posts: 2
thank you very much for your information.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
posted Nov 23, 2009 02:28:46
0
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.
subject: is there any API to access private members of a class?