Java.lang Package Classes

Java Class - getConstructors() Method



The java.lang.Class.getConstructors() method returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object. An array of length 0 is returned if the class has no public constructors, or if the class is an array class, or if the class reflects a primitive type or void.

Syntax

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

Parameters

No parameter is required.

Return Value

Returns the array of Constructor objects representing the public constructors of this class.

Exception

Throws SecurityException, if a security manager, s, is present and the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class.

Example:

The example below shows usage of the java.lang.Class.getConstructors() method.

import java.lang.*;
import java.lang.reflect.*;

public class MyClass {
  public static void main(String[] args) {
    try {
      Class cls = Class.forName("java.lang.Boolean");

      //print constructors
      Constructor c[] = cls.getConstructors();
      System.out.println("Constructors are: ");
      for(Constructor i: c)
        System.out.println(i);
        
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

The output of the above code will be:

Constructors are: 
public java.lang.Boolean(boolean)
public java.lang.Boolean(java.lang.String)

❮ Java.lang - Class

5