How do I a list of all of the class’ constructors?

Java Reflection allows us to inspect all the constructors of a given class and instantiate them at runtime. The Constructor class is in the Reflection package under java.lang.reflect.Constructor. It provides information about, and access to, a single constructor for a class.

Get a listing of all PUBLIC Constructors

In the following example we will use the object being passed into the method and get a list of all of the public constructors using the getConstructors() method. Then we iterate through the array of constructors and list each one out. The getConstructors() returns an array of constructor objects reflecting all public constructors of the class represented by the Class object.

public static void showAllPublicConstructors(Object obj) {
    Class<?> sa = obj.getClass();
    
    // Get all the public constructors
    System.out.println("Get all Declared Constructors...");
    Constructor<?>[] constructors = sa.getConstructors();

    // Let's iterate through all declared constructors...
    System.out.println("Iterating through all public constructors...");
    int i = 0;
    for (Constructor c : constructors) {
      System.out.printf("  [%d] ==> %s\n", ++i, c.toString());
    }
  }

Get a listing of all Declared Constructors

The getDeclaredConstructors() returns an array of constructor objects reflecting all declared constructors of the class represented by the Class object.

public static void showAllDeclaredConstructors(Object obj) {
    Class<?> sa = obj.getClass();
    
    // Get all the declared constructors
    System.out.println("Get all Declared Constructors...");
    Constructor<?>[] constructors = sa.getDeclaredConstructors();

    // Let's iterate through all declared constructors...
    System.out.println("Iterating through all declared constructors...");
    int i = 0;
    for (Constructor c : constructors) {
      System.out.printf("  [%d] ==> %s\n", ++i, c.toString());
    }
  }

Full Program Listing

package com.avaldes.tutorials;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ConstructorsExample {

  public static void showAllDeclaredConstructors(Object obj) {
    Class<?> sa = obj.getClass();
    
    // Get all the declared constructors
    System.out.println("Get all Declared Constructors...");
    Constructor<?>[] constructors = sa.getDeclaredConstructors();

    // Let's iterate through all declared constructors...
    System.out.println("Iterating through all declared constructors...");
    int i = 0;
    for (Constructor c : constructors) {
      System.out.printf("  [%d] ==> %s\n", ++i, c.toString());
    }
  }
  
public static void showAllPublicConstructors(Object obj) {
    Class<?> sa = obj.getClass();
    
    // Get all the declared constructors
    System.out.println("\nGet all Public Constructors...");
    Constructor<?>[] constructors = sa.getConstructors();

    // Let's iterate through all declared constructors...
    System.out.println("Iterating through all public constructors...");
    int i = 0;
    for (Constructor c : constructors) {
      System.out.printf("  [%d] ==> %s\n", ++i, c.toString());
    }
  }
  
  public static void main(String[] args)  {
    Horse majesty = new Horse();
    String s = "Hello";

    showAllDeclaredConstructors(majesty);
    showAllPublicConstructors(majesty);
    showAllPublicConstructors(s);
  }
}

Full Program Listing (Horse.java)

package com.avaldes.tutorials;

public class Horse extends Mammal implements Animal {
  private String stableName = null;
  
  public Horse() {
  }
  
  public Horse(String name) {
    setName(name); 
  }
  
  protected Horse(String name, boolean hair) {
    this.setName(name);
    this.setHair(hair);
  }
  
  private Horse(String name, String stableName) {
    this.setName(name);
    this.setStableName(stableName);
  }

  public String getStableName() {
    return stableName;
  }

  public void setStableName(String stableName) {
    this.stableName = stableName;
  }

  @Override
  public void eat() {
    System.out.println("Horse is eating...");
  }

  @Override
  public void sleep() {
    System.out.println("Horse is Sleeping...");
  }

  @Override
  public void walk() {
    System.out.println("Horse is Walking...");
  }
  
  public void trot() {
    System.out.println("Horse is Trotting...");
  }

  public void canter() {
    System.out.println("Horse is Cantering...");
  }

  public void gallop() {
    System.out.println("Horse is Galloping...");
  }
}

Output

Get all Declared Constructors...
Iterating through all declared constructors...
  [1] ==> public com.avaldes.tutorials.Horse(java.lang.String)
  [2] ==> protected com.avaldes.tutorials.Horse(java.lang.String,boolean)
  [3] ==> private com.avaldes.tutorials.Horse(java.lang.String,java.lang.String)
  [4] ==> public com.avaldes.tutorials.Horse()

Get all Public Constructors...
Iterating through all public constructors...
  [1] ==> public com.avaldes.tutorials.Horse(java.lang.String)
  [2] ==> public com.avaldes.tutorials.Horse()

Get all Public Constructors...
Iterating through all public constructors...
  [1] ==> public java.lang.String()
  [2] ==> public java.lang.String(java.lang.String)
  [3] ==> public java.lang.String(char[])
  [4] ==> public java.lang.String(char[],int,int)
  [5] ==> public java.lang.String(int[],int,int)
  [6] ==> public java.lang.String(byte[],int,int,int)
  [7] ==> public java.lang.String(byte[],int)
  [8] ==> public java.lang.String(java.lang.StringBuilder)
  [9] ==> public java.lang.String(byte[],int,int,java.lang.String) throws java.io.UnsupportedEncodingException
  [10] ==> public java.lang.String(byte[],int,int,java.nio.charset.Charset)
  [11] ==> public java.lang.String(byte[],java.lang.String) throws java.io.UnsupportedEncodingException
  [12] ==> public java.lang.String(byte[],java.nio.charset.Charset)
  [13] ==> public java.lang.String(byte[],int,int)
  [14] ==> public java.lang.String(byte[])
  [15] ==> public java.lang.String(java.lang.StringBuffer)

Java Reflection Examples

  • Class Example
    Simple example shows you how to dynamically load and instantiate a class, get class name, package name, and superclass name.
  • Modifier Example
    Showing you how to get information about classes, fields and methods using modifier method.
  • Method Example
    Providing full details of how to get all methods, and invoke methods using reflection.

Frequently Asked Questions on Java Reflection API (FAQs)

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *