Do subclasses inherit private fields?

Most of the confusion in the question/answers here surrounds the definition of Inheritance. Obviously, as @DigitalRoss explains an OBJECT of a subclass must contain its superclass’s private fields. As he states, having no access to a private member doesn’t mean its not there. However. This is different than the notion of inheritance for a class. … Read more

How to read the value of a private field from a different class in Java?

In order to access private fields, you need to get them from the class’s declared fields and then make them accessible: Field f = obj.getClass().getDeclaredField(“stuffIWant”); //NoSuchFieldException f.setAccessible(true); Hashtable iWantThis = (Hashtable) f.get(obj); //IllegalAccessException EDIT: as has been commented by aperkins, both accessing the field, setting it as accessible and retrieving the value can throw Exceptions, … Read more

Change private static final field using Java reflection

Assuming no SecurityManager is preventing you from doing this, you can use setAccessible to get around private and resetting the modifier to get rid of final, and actually modify a private static final field. Here’s an example: import java.lang.reflect.*; public class EverythingIsTrue { static void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true); Field modifiersField … Read more