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.


/* w w w.  ja  v a  2s. co  m*/
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.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable