Class clazz = MyClass.class; Constructor[] constructors = clazz.getDeclaredConstructors(); for (Constructor constructor : constructors) { System.out.println(constructor.getName()); }
Class clazz = MyClass.class; Constructor constructor = clazz.getDeclaredConstructor(int.class, String.class); MyClass instance = (MyClass) constructor.newInstance(10, "example");In this example, the getDeclaredConstructor() method is used to retrieve a constructor of MyClass that takes two arguments, an int and a String. The newInstance() method is then called on the constructor object with the appropriate arguments to create a new instance of MyClass. Package Library: The class getDeclaredConstructors() is part of the java.lang.reflect package, which is a part of the standard Java library.