views:

260

answers:

2

Hello

Is it possible to use getConstructor to obtain the constructor of the class X below?

public class A {
}

public class Y {

}

public class X extends Y {
    public X(A a, Y[] yy) {

    }
    public void someMethod() throws SecurityException, NoSuchMethodException {
        Class<? extends Y> clazz = X.class;
        Constructor<? extends Y> c =
            clazz.getConstructor(new Class[]{
                        A.class,
                        /* what do I put in here for the array of Ys? */
                    });
    }
}

Thanks

+3  A: 

You can construct class literals involving array notation just like you would with "undecorated" classes, namely ClassName[].class. This literal yields "the class which describes arrays of instances of ClassName". In your case:

clazz.getConstructor(new Class[] {
    A.class,
    Y[].class
 });
Hyque
+1  A: 

Or shorter.

    Constructor<X> c = X.class.getConstructor(A.class, Y[].class);
Peter Lawrey