Java SE API

Class.getDeclaredField()

Locate declared field metadata without granting access to its value. See official API for exceptions and overloads; Java 21-compatible example.

Formal Syntax & Structure

java syntaxjava
Field getDeclaredField(String name)

Parameters & Attributes

NameTypeDescription
nameStringDeclared field name

Return Value

Field

Locate declared field metadata without granting access to its value.

Working Example

Example Snippetjava
import java.lang.annotation.*;import java.lang.reflect.*;import java.lang.invoke.*;import java.util.*;import java.nio.file.*;import java.nio.charset.StandardCharsets;import java.net.URLClassLoader;import java.net.URL;public class Main {    public static class Parent { public String name() { return "report"; } }    public static class Report extends Parent {        public int pages = 3;        private int secret = 7;        public String format(int count) { return "pages=" + count; }        public void reject() { throw new IllegalStateException("invalid"); }        public List<String> tags = List.of("daily");    }    public static void main(String[] args) throws Throwable {        Field f = Report.class.getField("tags");        System.out.println(f.getType().getSimpleName());        System.out.println(f.getGenericType().getTypeName());        System.out.println(Report.class.getField("pages").get(new Report()));        System.out.println(Modifier.isPrivate(Report.class.getDeclaredField("secret").getModifiers()));    }}
Java Reference