Is it possible to instantiate a private inner class from another class using Java reflection. For example if I took this code
public class Main {
public
You can do the following :
public static void main(String[] args) throws Exception {
// to get first class in OtherClass
Class> innerClass = OtherClass.class.getDeclaredClasses()[0];
// getDeclaredConstructors for private constructor
Constructor> constructor = innerClass.getDeclaredConstructors()[0];
// to enable accessing private constructor
constructor.setAccessible(true);
OtherClass outerObject = new OtherClass();
//// create instance of Test by reflection
Object o = constructor.newInstance(outerObject);
System.out.println(o);
}