In object-oriented programming, a class constructor is a method called every time an object of a given class is created. In Java, a class can have more than one constructor, and different constructors differ in the list of parameters they accept. The java.lang.Class<T> class, which contains the definition of an arbitrary T class, offers various methods to inspect a class; one of these methods is getConstructors(), which returns an array of java.lang.reflect.Constructor objects representing each of the public constructors of a given class.

The constructor class allows retrieving information about a given constructor method, such as its parameters, the type of exceptions it can throw, etc. In the following example we demonstrate how to obtain the name of a constructor for the java.lang.String class, by using the getName() method of the Constructor class:


import java.lang.reflect.*;

public class GetConstructorsExample {
  public static void main(String[] args){
    Class cls = java.lang.String.class;
    Constructor constructor = cls.getConstructors()[0];

The above code takes the first element of the array returned by getConstructors() and retrieves the constructor name. When run, this program prints “java.lang.String” on the standard output: this is not surprising, since Java constructors are named after the class in which they are defined.