Skip to content

Instantly share code, notes, and snippets.

@nike-17
Created July 4, 2012 12:11
Show Gist options
  • Save nike-17/3047011 to your computer and use it in GitHub Desktop.
Save nike-17/3047011 to your computer and use it in GitHub Desktop.
public class Test {
private Test() {
System.out.println("private constructor has been called");
}
private void test() {
System.out.println("private test() method has been called");
}
}
public class Main {
public static void main(String[] args) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException,
InstantiationException, SecurityException, NoSuchMethodException {
Class<Test> clazz = Test.class;
Constructor<Test> c = clazz.getDeclaredConstructor((Class[]) null);
c.setAccessible(true);
Test test = c.newInstance((Object[]) null);
Method privateMethod = clazz.getDeclaredMethod("test", (Class[]) null);
privateMethod.setAccessible(true);
privateMethod.invoke(test, (Object[]) null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment