DevNotes
Support
Reflection · Chapter 07

Fields in Reflection

The Field API — getFields vs getDeclaredFields, field metadata, reading and modifying public, private, primitive, static and final fields, and how Jackson and Hibernate populate objects.

◷ 7 min read§ 16 sections? 7 interview questions{ } 18 code examples

#Introduction

In the previous chapter we learned how Reflection creates objects using constructors. Now we'll learn how Reflection can inspect, read and modify fields (instance variables) at runtime.

This is heavily used by Spring, Hibernate, Jackson, Gson and JPA providers. Without the Field API, these frameworks couldn't automatically populate Java objects.

#What is a Field?

A field is simply a variable declared inside a class.

Java
class Employee {
    private int id;
    public String name;
    protected double salary;
    static String company = "OpenAI";
}

id, name, salary and company are all fields. Reflection represents every field using java.lang.reflect.Field.

#Field Reflection Workflow

Class
Field
Read metadata
Read value
Modify value
Everything begins with the Class object

#Getting Fields

Java
public class Employee {
    public String name;
    private int age;
    protected double salary;
}

getFields() — Public Only

Java
Class<?> cls = Employee.class;

Field[] fields = cls.getFields();

for (Field field : fields) {
    System.out.println(field.getName());
}
Output
name

Only public fields are returned.

getDeclaredFields() — All Declared

Java
Field[] fields = Employee.class.getDeclaredFields();

for (Field field : fields) {
    System.out.println(field.getName());
}
Output
name
age
salary

Now private and protected fields are also returned.

getFields() vs getDeclaredFields()

MethodPublic fieldsPrivate fieldsInherited public fields
getFields()YesNoYes
getDeclaredFields()YesYesNo

#Getting a Specific Field

Java
class Employee {
    private String name;
}

Field field = Employee.class.getDeclaredField("name");

Now Reflection has access to the metadata of the name field.

#Field Metadata

Java
System.out.println(field.getName());   // name
System.out.println(field.getType());   // class java.lang.String

For private int age;, getType() prints int.

Modifiers:

Java
int modifier = field.getModifiers();

Modifier.isPrivate(modifier);
Modifier.isPublic(modifier);
Modifier.isStatic(modifier);
Modifier.isFinal(modifier);

#Reading Field Values

Public Field

Java
class Employee {
    public String name = "Rahul";
}

Employee emp = new Employee();

Field field = Employee.class.getField("name");
Object value = field.get(emp);

System.out.println(value);
Output
Rahul

Private Field

Java
class Employee {
    private String name = "Rahul";
}

Employee emp = new Employee();

Field field = Employee.class.getDeclaredField("name");
field.setAccessible(true);

System.out.println(field.get(emp));
Output
Rahul

Without setAccessible(true), reading the private field from another class throws IllegalAccessException.

#Modifying Field Values

Public Field

Java
class Employee {
    public String name = "Rahul";
}

Employee emp = new Employee();

Field field = Employee.class.getField("name");
field.set(emp, "Durga");

System.out.println(emp.name);
Output
Durga

Reflection successfully changed the value.

Private Field

Java
class Employee {
    private String name = "Rahul";
}

Employee emp = new Employee();

Field field = Employee.class.getDeclaredField("name");
field.setAccessible(true);
field.set(emp, "Durga");

System.out.println(field.get(emp));
Output
Durga

Reflection bypasses Java access checks (subject to module/security restrictions in modern Java).

#Primitive Fields

Java
class Employee {
    private int age = 25;
}

Employee emp = new Employee();

Field field = Employee.class.getDeclaredField("age");
field.setAccessible(true);

System.out.println(field.get(emp));
Output
25

The value is returned as an Object (an Integer).

Primitive-Specific Methods

Instead of field.get(emp) you can write:

Java
field.getInt(emp);
field.getDouble(emp);
field.getLong(emp);
field.getBoolean(emp);
field.getFloat(emp);

field.setInt(emp, 30);
field.setDouble(emp, 50000);
field.setBoolean(emp, true);

These avoid boxing and unboxing.

#Static Fields

Java
class Employee {
    static String company = "OpenAI";
}

Field field = Employee.class.getDeclaredField("company");
field.setAccessible(true);

System.out.println(field.get(null));
Output
OpenAI

For static fields the object reference is null, because static members belong to the class, not an object.

#Final Fields

Java
class Employee {
    final String company = "OpenAI";
}

Reflection can sometimes modify final fields depending on the Java version and JVM configuration, but this is strongly discouraged and increasingly restricted in modern Java. In production, assume final fields stay immutable.

#Real-World Examples

Jackson

Suppose JSON:

JSON
{
  "name": "Rahul",
  "age": 25
}

When Jackson is set up to use fields, it performs operations conceptually similar to:

Java
Employee emp = new Employee();

Field field = Employee.class.getDeclaredField("name");
field.setAccessible(true);
field.set(emp, "Rahul");

Field age = Employee.class.getDeclaredField("age");
age.setAccessible(true);
age.set(emp, 25);

The object is populated dynamically.

Hibernate

IDNAME
1Rahul
Java
Employee emp = new Employee();

Field id = Employee.class.getDeclaredField("id");
id.setAccessible(true);
id.set(emp, 1);

Field name = Employee.class.getDeclaredField("name");
name.setAccessible(true);
name.set(emp, "Rahul");

Without Reflection, ORM frameworks would require manual setter calls for every field. (Hibernate uses field access when @Id is on a field and property/setter access when it's on a getter.)

#Exceptions

ExceptionReason
NoSuchFieldExceptionField not found
IllegalAccessExceptionAccess denied
SecurityExceptionAccess restricted by a security policy
Java
try {
    Field field = Employee.class.getDeclaredField("name");
} catch (Exception e) {
    e.printStackTrace();
}

#Interview Questions

Q1Which class represents fields in Reflection?

java.lang.reflect.Field.

Q2Difference between getField() and getDeclaredField()?
MethodPublicPrivate
getField()✓ (including inherited)✗
getDeclaredField()✓ (this class only)✓
Q3Difference between getFields() and getDeclaredFields()?
  • getFields() — public fields only, including inherited public fields.
  • getDeclaredFields() — all fields declared in the class, excluding inherited fields.
Q4Which method reads a field value?

field.get(object).

Q5Which method modifies a field value?

field.set(object, value).

Q6Why is setAccessible(true) required?

It allows Reflection to access non-public fields. Without it, private field access normally results in an IllegalAccessException.

Q7How do you access a static field?

Pass null to field.get() or field.set() — e.g. field.get(null).

#Key Points to Remember

  • Fields are represented by java.lang.reflect.Field.
  • Reflection can inspect both public and private fields.
  • getField() returns only public fields; getDeclaredField() returns any declared field.
  • field.get() reads values; field.set() modifies them.
  • setAccessible(true) enables access to private fields (subject to modern Java restrictions).
  • Primitive-specific methods such as getInt() and setInt() avoid boxing.
  • Static fields use null as the target object.

#Chapter Summary

The Field API enables Reflection to inspect class variables, retrieve metadata, and read and modify values dynamically at runtime. This is fundamental to serialization frameworks, dependency injection containers and ORM technologies like Hibernate, and explains how frameworks populate objects without directly invoking setters.