my dog learned polymorphism
The moose likes Beginning Java and the fly likes is there any API to access private members of a class? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "is there any API to access private members of a class?" Watch "is there any API to access private members of a class?" New topic
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
    
    7
No. The point of private members is that they can't be accessed from outside of the class.


Android appsImageJ pluginsJava 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
    
    7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is there any API to access private members of a class?
 
Similar Threads
about access specifiers
Difference between public and private
Private member access
Accessing private inner class members
Changing value of Private Var outside its class