getConstructors()

  • How to retrieve the constructors of a Java class

    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. Thejava.lang.Class<T> class, which contains the definition of an arbitraryT class, offers various methods to inspect a class; one of these methods isgetConstructors(), which returns an array ofjava.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 thejava.lang.String class, by using the getName() method of the Constructor class: