How do I get field of a class object and set or get its value?

A refection demo to get fields of a class’s object and set or get their values.

package org.kodejava.lang.reflect;

import java.util.Date;
import java.lang.reflect.Field;

public class GetSetFieldDemo {
    public static Date now;
    public Long id;
    public String name;

    public static void main(String[] args) {
        GetSetFieldDemo demo = new GetSetFieldDemo();
        Class<? extends GetSetFieldDemo> clazz = demo.getClass();

        try {
            // Get field id, set it value and read it back
            Field field = clazz.getField("id");
            field.set(demo, 10L);
            Object value = field.get(demo);
            System.out.println("Value = " + value);

            // Get static field date, set it value and read it back
            field = clazz.getField("now");
            field.set(null, new Date());
            value = field.get(null);
            System.out.println("Value = " + value);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }

    }
}

The output of the code snippet:

Value = 10
Value = Wed Oct 06 06:18:23 CST 2021

How do I get fields of a class object?

The example below using reflection to obtain the fields of a class object. We’ll get the field names and their corresponding type. There are three ways shown below which can be used to get an object fields:

  • Class.getDeclaredFields()
  • Class.getFields()
  • Class.getField(String)
package org.kodejava.lang.reflect;

import java.util.Date;
import java.lang.reflect.Field;

public class GetFields {
    public Long id;
    protected String name;
    private Date birthDate;
    Double weight;

    public static void main(String[] args) {
        GetFields object = new GetFields();
        Class<? extends GetFields> clazz = object.getClass();

        // Get all object fields including public, protected, package and private
        // access fields.
        Field[] fields = clazz.getDeclaredFields();
        System.out.println("Number of fields = " + fields.length);
        for (Field field : fields) {
            System.out.println("Field name = " + field.getName());
            System.out.println("Field type = " + field.getType().getName());
        }

        System.out.println("\n----------------------------------------\n");

        // Get all object accessible public fields.
        fields = clazz.getFields();
        System.out.println("Number of fields = " + fields.length);
        for (Field field : fields) {
            System.out.println("Field name = " + field.getName());
            System.out.println("Field type = " + field.getType().getName());
        }

        System.out.println("\n----------------------------------------\n");

        try {
            // Get field name id with public access modifier
            Field field = clazz.getField("id");
            System.out.println("Field name = " + field.getName());
            System.out.println("Field type = " + field.getType().getName());
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}

The output of the code snippet above are:

Number of fields = 4
Field name = id
Field type = java.lang.Long
Field name = name
Field type = java.lang.String
Field name = birthDate
Field type = java.util.Date
Field name = weight
Field type = java.lang.Double

----------------------------------------

Number of fields = 1
Field name = id
Field type = java.lang.Long

----------------------------------------

Field name = id
Field type = java.lang.Long