Ok, I modified my code.
I was wrong about no providing the proper arguments to the
constructor.
That works now, but I am not able to invoke newInstance().
Here is the modified code :
****
import java.lang.reflect.*;
public class Reflection {
private class InnerClass1 {
//default constructor
public InnerClass1(int num) {}
public String description() {
return "InnerClass 1";
}
}
public static void main(String[] args) throws ClassNotFoundException,
NoSuchMethodException,
InstantiationException, IllegalAccessException,
InvocationTargetException
{
//trying to create a new instance of the inner class
//code compiled with no syntax errors
Class cls = Class.forName("Reflection$" + "InnerClass1");
Class[] partypes;
partypes = new Class[1];
partypes[0] = Integer.class;
Object[] arglist;
arglist = new Object[1];
arglist[0] = new Integer(500);
Constructor[] cons = cls.getConstructors();
System.out.println(cons[0]);
Object methobj = cons[0].newInstance(arglist);
}
}
****
Thanks again for you help,
gk
> (learningjava) writes:
>
> > public InnerClass1() { }
>
> Do you see a "long xxx" inside those parentheses? Neither do we.
>
> > Class[] partypes;
> > partypes = new Class[1];
> > partypes[0] = Long.TYPE;
> > //this throws a NoSuchMethodException (runtime error). why?
> > //the exception text also has something like <init>
> > //what does it mean?
> > Constructor con = cls.getConstructor(partypes);
>
> It means that despite your insistence you haven't written a
> constructor thast takes a long parameter.