import java.lang.reflect.Constructor; public class MyClass { public MyClass() {} public MyClass(int a) {} public MyClass(String s) {} public static void main(String[] args) { Constructor[] constructors = MyClass.class.getConstructors(); for (Constructor c : constructors) { System.out.println(c); } } }
import java.lang.reflect.Constructor; public class MyClass { public MyClass(int a, String s) { System.out.println("a = " + a + ", s = " + s); } public static void main(String[] args) throws Exception { ConstructorIn this example, the `getConstructor()` method is used to get a `Constructor` object for the `MyClass` constructor that takes an `int` and a `String` as arguments. Then, the `newInstance()` method is called on the `Constructor` object to create a new instance of the `MyClass` class with the specified arguments. Package library: `java.lang.reflect`constructor = MyClass.class.getConstructor(int.class, String.class); MyClass obj = constructor.newInstance(42, "Hello World!"); } }