Accessing private members [closed]

Let’s not consider ethics for a moment. Let’s consider the standard. What you are proposing to do is nonstandard. See section 9.2, clause 12 of the standard. “The order of allocation of nonstatic members separated by an access-specifier is unspecified.” Therefore, if you have a class with private members, and a struct with no private … Read more

Access to private inherited fields via reflection in Java

This should demonstrate how to solve it: import java.lang.reflect.Field; class Super { private int i = 5; } public class B extends Super { public static void main(String[] args) throws Exception { B b = new B(); Field f = b.getClass().getSuperclass().getDeclaredField(“i”); f.setAccessible(true); System.out.println(f.get(b)); } } (Or Class.getDeclaredFields for an array of all fields.) Output: 5