I am trying to get getConstructor to work. I'm having problems.
Here is my test code:
import java.lang.reflect.*;
public class ConstructorTest {
public static void main(String[] args) {
Class klass = String.class;
Class c = CC.class;
String title = klass.getName();
try {
Constructor cnstr = klass.getConstructor(new Class[] {String.class});
//Constructor cnstr2 = c.getConstructor(new Class[] {String.class});
System.out.println(cnstr.newInstance(new Object[] { title }));
} catch (Exception e) {
System.out.println(e);
}
}
}
class CC {
CC(String s) {}
}
I can get the constructor for java.lang.String, but when I try to do the
same for CC, I get NoSuchMethodException. What am I missing?
TIA
opalpa@gmail.com opalinski from opalpaweb - 28 Mar 2006 00:17 GMT
I believe that CC's constructor needs be public.
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/