Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Field — reflective field access in Java

java.lang.reflect.Field describes one field and enables access to its value at runtime. It supports inspecting metadata and annotations and changing values, subject to access-control and module-system restrictions.

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

Syntax

Field f = Type.class.getDeclaredField("name");
f.getName(); f.getType();
f.setAccessible(true);
f.set(target, value); Object valueRead = f.get(target);
f.setInt(target, 42); int intRead = f.getInt(target);
f.getAnnotation(Mark.class); f.isAnnotationPresent(Mark.class);

java.lang.reflect.Field represents an instance or static field. Class.getDeclaredField finds a field declared in that class, including private fields but excluding inherited fields. Class.getField finds public fields, including inherited ones. A missing field causes NoSuchFieldException; finding it does not grant access.

Field.getName() returns the name; Field.getType() returns the declared type as Class<?>. Field.get(target) boxes primitive results; Field.set(target, value) unboxes values for primitive fields. Field.getInt(target) reads int or a type widenable to int; Field.setInt(target, value) writes an int, with widening if needed. Invalid conversions or receivers cause IllegalArgumentException. For instance fields, target == null causes NullPointerException; static fields ignore the receiver.

Field.setAccessible(true) requests suppression of language access checks, not a bypass of module boundaries: private cross-module access requires the package to be open to the caller's module. Denial can cause InaccessibleObjectException or SecurityException; inaccessible reads or writes cause IllegalAccessException. Do not treat this method as general permission to modify final fields.

Field.getAnnotation(Mark.class) returns a declaration annotation or null; the inherited method Field.isAnnotationPresent(Mark.class) reports its presence as a boolean. An annotation needs RetentionPolicy.RUNTIME to be readable through reflection. The example uses a private, mutable field and an explicitly retained annotation.

Examples

import java.lang.annotation.*;
import java.lang.reflect.Field;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Mark {}

class Counter {
    @Mark
    private int count = 1;
}

public class Main {
    public static void main(String[] args) throws ReflectiveOperationException {
        Counter target = new Counter();
        Field f = Counter.class.getDeclaredField("count");
        System.out.println(f.getName() + ":" + f.getType().getName());
        f.setAccessible(true);
        f.set(target, 7);
        System.out.println(f.get(target));
        f.setInt(target, 9);
        System.out.println(f.getInt(target));
        System.out.println(f.isAnnotationPresent(Mark.class));
        System.out.println(f.getAnnotation(Mark.class) != null);
    }
}

Result

count:int
7
9
true
true

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