0

I'm learning reflection in Java and I write some test code:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test  {
    class Base {
        public Base() {}
        public void print(){
            System.out.println("base");
        }
    }

    class Derived extends Base {
        @Override
        public void print() {
            System.out.println("derived");
        }
    }

    public static void main(String args[])
    {
        try {
            Class.forName(Derived.class
                    .getTypeName())
                    .getSuperclass()
                    .getMethod("print", new Class[0])
                    .invoke(Base.class.newInstance());// line 41
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
}

But when I run this code, I get:

java.lang.InstantiationException: Test$Base
    at java.lang.Class.newInstance(Class.java:427)
    at Test.main(Test.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NoSuchMethodException: Test$Base.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 6 more

Could any one please tell me why? The constructor of the class base is public but the compiler still claims can't find its constructor..

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
Sayakiss
  • 6,878
  • 8
  • 61
  • 107
  • I think it's because `Base` is instance class, which can't be created without an instance of `Test`...I think...If you make `Base` and `static` inner class, it will work – MadProgrammer Jul 14 '15 at 02:47
  • Take a look at [this question/answer](http://stackoverflow.com/questions/17485297/how-to-instantiate-inner-class-with-reflection-in-java) for the solution to instantiate a inner instance class using reflection... – MadProgrammer Jul 14 '15 at 02:50

1 Answers1

10

Because Base is an inner class and all (*) inner class constructors implicitly declare a formal parameter of the enclosing class at index 0.

The constructor of a (*) non-private inner member class implicitly declares, as the first formal parameter, a variable representing the immediately enclosing instance of the class (§15.9.2, §15.9.3).

In other words, it's not a parameterless constructor. You'll need to use Class#getConstructor(Class[]) to get the appropriate constructor, then invoke it.

Base instance = Base.class.getConstructor(Test.class).newInstance(new Test());
Class.forName(Derived.class.getTypeName()).getSuperclass().getMethod("print", new Class[0]).invoke(instance);

(All this to say that inner classes are hard to work with.)

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724