-2

is it possible to read private fields using Java reflection?

Amol Patil
  • 985
  • 2
  • 11
  • 43

1 Answers1

1

You can use the Apcache commons FieldUtils

FieldUtils.readField(object, myfield, true);

or else you can use the Reflection as is described and answered in the linked duplicate. So you can set the setAccessible(true) before invoking your method.

m = object.getClass().getDeclaredMethod(mymethod);
m.setAccessible(true);
m.invoke(object);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331