Java Reflection - Java Class .getDeclaredConstructors ()








Syntax

Class.getDeclaredConstructors() has the following syntax.

public Constructor <?>[] getDeclaredConstructors()      throws SecurityException

Example

In the following code shows how to use Class.getDeclaredConstructors() method.

//  www  . ja  v a  2 s .c om
import java.lang.reflect.Constructor;

public class Main {

   public static void main(String[] args) {

     MyClass cls = new MyClass();
     Class c = cls.getClass();

     // constructors
     Constructor[] ct = c.getDeclaredConstructors();
     for(int i = 0; i < ct.length; i++) {
        System.out.println("Constructor found: " + ct[i].toString());
     }
   }
}

class MyClass{
   public MyClass() {
      System.out.println("no argument constructor");
   }

   public MyClass(Double d, Long l) {
      this.d = d;
      this.l = l;
   }

   Double d = new Double(3.9d);
   Long l = new Long(123);
}

The code above generates the following result.