Good afternoon. There is an instance of some inner class that has a constructor with a parameter . I.e:
class A{ ... class B{ B(int i){ } } ... class C{ C(int i){ } } ... } How to use reflection to create another instance of this class?
Good afternoon. There is an instance of some inner class that has a constructor with a parameter . I.e:
class A{ ... class B{ B(int i){ } } ... class C{ C(int i){ } } ... } How to use reflection to create another instance of this class?
Class<?> enclosingClass = Class.forName("A"); Object enclosingInstance = enclosingClass.newInstance(); Class<?> innerClass = Class.forName("A$B"); Constructor<?> ctor = innerClass.getDeclaredConstructor(enclosingClass, int.class); Object innerInstance = ctor.newInstance(enclosingInstance, 42); Source: https://ru.stackoverflow.com/questions/830759/
All Articles