Getting a list of all constructors of a class through reflection

Call getDeclaredConstructors on a Class object. There is another method called getConstructors but this one returns only public constructors.

Main.java:

import java.lang.reflect.*;
 
public class Main {
   public static void main(String args[]) throws Exception {
      Class c = Class.forName("MyClass");   // or Class c = MyClass.getClass();
 
      Constructor[] constructors = c.getDeclaredConstructors();
      for (int i=0; i<constructors.length; i++) {
         System.out.println(constructors[i]);
      }   
   }
}
 
class MyClass
{
   public MyClass() { }
   public MyClass(int a) { }
   protected MyClass(float f) { }
   private MyClass(String s) { }
}

outputs:

public MyClass()
public MyClass(int)
protected MyClass(float)
private MyClass(java.lang.String)