import java.lang.reflect.*; import java.util.List; class Analyzer { int n = 0; static boolean shared = true; boolean flag = false; static String name = "Smith"; void launchTheMissiles(int numOfMissiles) { // ... } String makeCoffee(List managers) { // type erasure, the type argument Object will be deleted // ... return "Here is your coffee."; } static void analyze(Object o) { System.out.println("Contents of " + o.getClass()); Class cls = o.getClass(); // '?' means "I don't know which class I am looking at" // it could be a String, a ClassInspector, an Analyzer or something // completely different class Field[] fields = cls.getDeclaredFields(); System.out.println(" static fields:"); for (Field field : fields) if (Modifier.isStatic(field.getModifiers())) System.out.println(" " + field); System.out.println(" non-static fields:"); for (Field field : fields) if (!Modifier.isStatic(field.getModifiers())) System.out.println(" " + field); Method[] methods = cls.getDeclaredMethods(); System.out.println(" non-static methods:"); for (Method method : methods) if (!Modifier.isStatic(method.getModifiers())) System.out.println(" " + method); System.out.println(); } public static void main(String[] args) { analyze("hello"); analyze(new ClassInspector()); analyze(new Analyzer()); String s = "world"; // Class stringCls = s.getClass(); // It is not as simple as that... :( Class stringCls = String.class; // Special syntax for getting Class } }