Java SE API
Class.getField()
Find a public field including inherited fields. See official API for exceptions and overloads; Java 21-compatible example.
Formal Syntax & Structure
java syntaxjava
Field getField(String name)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | String | Public field name |
Return Value
Field
Find a public field including inherited fields.
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())); }}