Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to invoke a non-static method you need to tell on which object you want to invoke it. In your case you want inner class object, but since you don't have one yet you need to create it. Since Java can't let inner class object be created without outer class object you will need to create that outer object too.</p> <p>So these are steps you need to take:</p> <ul> <li>create outer class object (if you don't have one), </li> <li>using outer class object create inner class object, </li> <li>invoke method on inner class object. </li> </ul> <p>You can do it like this:<br> (you just need to remember that default constructors have same visibility as visibility of its class, so private class will have private constructor which we will need to make accessible before we will be able to use it)</p> <pre><code>try { //creating parent object Object outer = new MyBigClass(); //creating inner class object Class&lt;?&gt; innerClass = Class.forName("MyBigClass$MyInnerClass"); Constructor&lt;?&gt; constructor = innerClass.getDeclaredConstructor(MyBigClass.class);//inner object must know type of outer class constructor.setAccessible(true);//private inner class has private default constructor Object child = constructor.newInstance(outer);//inner object must know about its outer object //invoking method on inner class object Method method = innerClass.getDeclaredMethod("wantedMethod",new Class&lt;?&gt;[]{}); method.setAccessible(true);//in case of unaccessible method method.invoke(child,new Object[]{}); } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } </code></pre> <p>You can find more info about creating inner class object via reflection in <a href="https://stackoverflow.com/questions/2097982/is-it-possible-to-create-an-instance-of-nested-class-using-java-reflection">this question</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload