import java.lang.reflect.*; class ObjectInspector { public static void inspect(Object o) throws IllegalAccessException { // Get class and fields for object Class c = o.getClass(); while (c != Object.class) { System.out.println("// Fields in " + c); Field[] fields = c.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); // Print field values for (int i = 0; i < fields.length; ++i) { Field fld = fields[i]; System.out.print(fld.getName() + ": "); System.out.println(fld.get(o)); } System.out.println(); c = c.getSuperclass(); } } public static void main(String[] args) { try { inspect(new java.util.TreeMap()); } catch (IllegalAccessException x) { System.out.println(x); } } } /* Output: // Fields in class java.util.TreeMap comparator: null root: null size: 0 modCount: 0 keySet: null entrySet: null values: null KEYS: 0 VALUES: 1 ENTRIES: 2 RED: false BLACK: true serialVersionUID: 919286545866124006 // Fields in class java.util.AbstractMap keySet: null values: null */ End of Listing