RawSushi's Tech Journal
My online tech scrapbook where I keep my notes in case I need to look them up later
Sunday, August 19, 2012
Java Reflection: Access a Private Field
It is possible to access private fields in Java classes using the Reflection API (as long as the Security Manager isn't configured to prevent this).
This is done by adding "field.setAccessible(true);" before trying to obtain the value of the field.
Example code:
Field field = SomeClass.class.getDeclaredField(fieldName);
feld.setAccessible(true);
return field.get(obj);
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment