Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Constructor — inspecting and invoking constructors through reflection

java.lang.reflect.Constructor<T> describes one constructor of class T. It exposes parameter types and annotations and can create objects, subject to access checks and Java SE 21 module boundaries.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

Constructor<T> ctor = type.getDeclaredConstructor(parameterTypes);
Class<?>[] types = ctor.getParameterTypes();
boolean annotated = ctor.isAnnotationPresent(annotationType);
ctor.setAccessible(true);
T instance = ctor.newInstance(arguments);

java.lang.reflect.Constructor<T> represents one constructor of class T, not a constructed object. Class.getConstructor(...) finds a public constructor, whereas Class.getDeclaredConstructor(...) also finds non-public constructors declared by that class. Lookup requires exact parameter types: int.class and Integer.class are not interchangeable. Finding a constructor does not itself grant permission to invoke it.

Constructor.getParameterTypes() returns a Class<?>[] array in declaration order; it is empty for a constructor with no parameters. It can include implicit parameters, such as the enclosing instance for a non-static inner class. The inherited Constructor.isAnnotationPresent(...) checks a constructor annotation, not its parameter annotations. An annotation needs RetentionPolicy.RUNTIME to be available through reflection.

Constructor.setAccessible(true) requests suppression of Java language access checks; it does not change the constructor declaration. It cannot arbitrarily bypass module boundaries: accessing a private constructor in another module requires its package to be open to the calling module. Denial can produce InaccessibleObjectException or SecurityException. setAccessible(false) restores checks, while trySetAccessible() can return false when access cannot be enabled. Prefer accessible public APIs over relying on private library constructors.

Constructor.newInstance(...) creates and initializes an object. It supports argument unboxing and permitted widening conversions, but not narrowing conversions. Invalid arguments cause IllegalArgumentException, inaccessible constructors cause IllegalAccessException, and abstract classes cause InstantiationException. An exception thrown by the constructor is wrapped in InvocationTargetException; inspect it with getCause().

The example uses a separate top-level class Item, so Main has no special nestmate access to its private constructor. On the class path, setAccessible(true) enables invocation. This entry targets Java SE 21; the production runtime has not been attested.

Examples

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.util.Arrays;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.CONSTRUCTOR)
@interface Selected {}

final class Item {
    final String name;

    @Selected
    private Item(String name) {
        this.name = name;
    }
}

public class Main {
    public static void main(String[] args) throws ReflectiveOperationException {
        Constructor<Item> ctor = Item.class.getDeclaredConstructor(String.class);
        System.out.println(Arrays.toString(ctor.getParameterTypes()));
        System.out.println(ctor.isAnnotationPresent(Selected.class));
        ctor.setAccessible(true);
        Item item = ctor.newInstance("Ada");
        System.out.println(item.name);
    }
}

Result

[class java.lang.String]
true
Ada

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources