F
frank
I'm trying to create a class instance dynamically using reflection. I
can do it if I want to use the default constructor (i.e. no parameters),
and I also found an example how to have it call the constructor with
parameters if the parameters are a privative JAVA type
import java.lang.reflect.*;
public class constructor2 {
public constructor2()
{
}
public constructor2(int a, int b)
{
System.out.println(
"a = " + a + " b = " + b);
}
public static void main(String args[])
{
try {
Class cls = Class.forName("constructor2");
Class partypes[] = new Class[2];
partypes[0] = Integer.TYPE; //i.e. would be my own class
partypes[1] = Integer.TYPE;
Constructor ct
= cls.getConstructor(partypes);
Object arglist[] = new Object[2];
arglist[0] = new Integer(37);
arglist[1] = new Integer(47);
Object retobj = ct.newInstance(arglist);
}
catch (Throwable e) {
System.err.println(e);
}
}
}
But how do I pass/call a construtor where the patameters is not Integer?
In fact it's one of my own classes?
Thanks,
Frank
can do it if I want to use the default constructor (i.e. no parameters),
and I also found an example how to have it call the constructor with
parameters if the parameters are a privative JAVA type
import java.lang.reflect.*;
public class constructor2 {
public constructor2()
{
}
public constructor2(int a, int b)
{
System.out.println(
"a = " + a + " b = " + b);
}
public static void main(String args[])
{
try {
Class cls = Class.forName("constructor2");
Class partypes[] = new Class[2];
partypes[0] = Integer.TYPE; //i.e. would be my own class
partypes[1] = Integer.TYPE;
Constructor ct
= cls.getConstructor(partypes);
Object arglist[] = new Object[2];
arglist[0] = new Integer(37);
arglist[1] = new Integer(47);
Object retobj = ct.newInstance(arglist);
}
catch (Throwable e) {
System.err.println(e);
}
}
}
But how do I pass/call a construtor where the patameters is not Integer?
In fact it's one of my own classes?
Thanks,
Frank