0

I am using a third party Java class to fetch an instance of the class. However, only a few variables can be fetched using respective methods. I can see other variables when I print the instance and I would like to fetch some of them. Is there a Java way to do that.

  • 1
    You can refer this https://stackoverflow.com/questions/1196192/how-to-read-the-value-of-a-private-field-from-a-different-class-in-java – Vikas Jan 07 '21 at 09:40

1 Answers1

2

As it was pointed out in comment. You can refer to this answer. And use reflection in this way:

Field f = thirdPartyObject.getClass().getDeclaredField("hiddenString"); //NoSuchFieldException
f.setAccessible(true);
String hiddenString = (String) f.get(thirdPartyObject); //IllegalAccessException
lczapski
  • 4,026
  • 3
  • 16
  • 32