Below is an example that showing you how to get constructors of a class object. In the code below we get the constructors by calling the Class.getDeclaredConstructors()
or the Class.getConstructor(Class[])
method.
package org.kodejava.lang.reflect;
import java.lang.reflect.Constructor;
public class GetConstructors {
public static void main(String[] args) {
Class<String> clazz = String.class;
// Get all declared constructors and iterate the constructors to get their
// name and parameter types.
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
for (Constructor<?> constructor : constructors) {
String name = constructor.getName();
System.out.println("Constructor name= " + name);
Class<?>[] parameterTypes = constructor.getParameterTypes();
for (Class<?> c : parameterTypes) {
System.out.println("Param type name = " + c.getName());
}
System.out.println("----------------------------------------");
}
// Getting a specific constructor of the java.lang.String
try {
Constructor<String> constructor = String.class.getConstructor(String.class);
System.out.println("Constructor = " + constructor.getName());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
Latest posts by Wayan (see all)
- How do I create a table with multiple header in iText 8? - March 16, 2024
- How do I add Image to a Table in iText 8? - March 11, 2024
- How do I downgrade Android SDK emulator version? - February 12, 2024