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