import java.lang.reflect.Field; import java.lang.reflect.Method; /** * @author Chuck Caplan * */ public class PrivateTest { public static void main(String[] args) { try { Field f = Private.class.getDeclaredField("intPrivate"); f.setAccessible(true); // This line is what allows us to access // private methods and fields. System.out.println(f.getInt(new Private())); } catch (Exception e) { e.printStackTrace(); } try { Field f = Private.class.getDeclaredField("strPrivate"); f.setAccessible(true); // This line is what allows us to access // private methods and fields. System.out.println(f.get(new Private())); } catch (Exception e) { e.printStackTrace(); } try { Method m = Private.class.getDeclaredMethod("voidPrivate", null); m.setAccessible(true); // This line is what allows us to access // private methods and fields. m.invoke(new Private(), null); } catch (Exception e) { e.printStackTrace(); } } }