You should pass the object to get method of the field, so

  import java.lang.reflect.Field;

  Field field = object.getClass().getDeclaredField(fieldName);    
  field.setAccessible(true);
  Object value = field.get(object);
Answer from Dmitry Spikhalsky on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › retrieve fields from a java class using reflection
Retrieve Fields from a Java Class Using Reflection | Baeldung
January 4, 2026 - In this case, we can use the Java Reflection API method Class::getSuperclass(). The .getSuperClass() method returns the superclass of our class without referring explicitly to the name of the superclass.
🌐
Oracle
docs.oracle.com › javase › tutorial › reflect › member › fieldValues.html
Getting and Setting Field Values (The Java™ Tutorials > The Reflection API > Members)
$ java Book BEFORE: chapters = 0 AFTER: chapters = 12 BEFORE: characters = [Alice, White Rabbit] AFTER: characters = [Queen, King] BEFORE: twin = DEE AFTER: twin = DUM · Note: Setting a field's value via reflection has a certain amount of performance overhead because various operations must occur such as validating access permissions.
🌐
TutorialsPoint
tutorialspoint.com › javareflect › javareflect_field_get.htm
java.lang.reflect.Field.get() Method Example
package com.tutorialspoint; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Field; public class FieldDemo { public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { SampleClass sampleObject = new SampleClass(); sampleObject.setSampleField("data"); Field field = SampleClass.class.getField("sampleField"); System.out.println(field.get(sampleObject)); } } @CustomAnnotation(name = "SampleClass", value = "Sample Class Annotation") class SampleClass { @CustomAn
🌐
Baeldung
baeldung.com › home › java › core java › get all record fields and its values via reflection
Get All Record Fields and Its Values via Reflection | Baeldung
January 16, 2024 - However, the problem can be solved without using the new RecordComponent class. Java reflection API provides the Class.getDeclaredFields() method to get all declared fields in the class.
Top answer
1 of 3
12

Since I don't know what exactly is stopping you from writing your code here are some tools which should be helpful:

  • to get array of fields declared in class use

    Field[] fields = User.class.getDeclaredFields()
    
  • to check what is the type assigned to field use field.getType().

  • to check if type is same as other type like List simply use

    type.equals(List.class);
    
  • to check if one type belongs to family of some ancestor (like List is subtype of Collection) use isAssignableFrom like

    Collection.class.isAssignableFrom(List.class)
    
  • to check modifiers of field like transient use

    Modifier.isTransient(field.getModifiers())
    
  • to access value held by field in specific instance, use

    Object value = field.get(instanceWithThatField)
    

    but in case of private fields you will need to make it accessible first via field.setAccessible(true). Then if you are sure about what type of object value holds you can cast it to that type like

    List<Account> list = (List<Account>) value;
    

    or do both operations in one line, like in your case

    List<Account> list = (List<Account>) field.get(userObject);
    

    which you can later iterate the way you want like
    for(Account acc : list){ /*handle each acc*/ }

2 of 3
1

Elaborating on @Pshemo's answer, Here are the steps: (Please note its easier to get specific Type parameters from a Class than an Object, due to Type erasures.)

  1. Loop through the fields in User.
  2. For fields with predefined types check transcience.
  3. For Collections, get the type of of its parameter and if its a custom Type, run a recursion on that class.

e.g

    private static void checkTranscience(Class<?> clazz) {
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        System.out.println("Transience " + Modifier.isTransient(field.getModifiers()) + " for " + field.getName());
        Class<?> fieldClass;
        if (Collection.class.isAssignableFrom(field.getType())) {
            //In case of Parameterized List or Set Field, extract genericClassType
            fieldClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
        } else {
            fieldClass = field.getType();
        }
        //Assuming Account belongs to "com.examplepackage.accountpackage" package to narrow down recursives to custom Types only
        if (fieldClass.getName().contains("com.examplepackage.accountpackage")) {
            checkTranscience(fieldClass);
        }
    }


class User {
    private String x;
    private List<String> stringList;
    private List<Account> accounts;
    private int y;
}

public static void main(){
    checkTranscience(User.class);
}

You might have to tune the code a bit to get the specific result, but I'll leave that to the user.

🌐
Netjstech
netjstech.com › 2017 › 07 › reflection-in-java-field.html
Reflection in Java - Getting Field Information | Tech Tutorials
December 23, 2021 - public String name = "Test"; Class<?> c = Class.forName("org.netjs.prog.ReflectField"); ReflectField rf = new ReflectField(); Field f = c.getField("name"); // getting the field value System.out.println("Value of field name " + f.get(rf)); // setting the new field value f.set(rf, "New Value"); ...
Find elsewhere
🌐
Jenkov
jenkov.com › tutorials › java-reflection › fields.html
Java Reflection - Fields
April 21, 2016 - Here is an example: Class aClass = ...//obtain class object Field[] fields = aClass.getFields(); The Field[] array will have one Field instance for each public field declared in the class.
🌐
Tabnine
tabnine.com › home page › code › java › org.reflections.reflectionutils
org.reflections.ReflectionUtils.getFields java code examples | Tabnine
@SuppressWarnings("unchecked") private List<Object> getFingerprintableValues() { final List<Object> fieldValues = new ArrayList<>(); try { final Set<Field> fields = ReflectionUtils.getFields(getClass()); for (final Field field : fields) { field.setAccessible(true); final Object value = field.get(this); if ((value != null) && !isAnnotationPresent(field, IgnoreFingerprint.class, Version.class, Transient.class)) { fieldValues.add(value); } } return fieldValues; } catch (final Exception e) { throw new RuntimeException(e); } } origin: com.atlassian.applinks/applinks-test-common ·
🌐
W3Docs
w3docs.com › java
Reflection generic get field value
To get the value of a generic field using reflection in Java, you can use the get() method of the Field class, which returns the value of the field as an Object.
🌐
Avajava
avajava.com › tutorials › lessons › how-do-i-get-and-set-a-field-using-reflection.html
How do I get and set a field using reflection?
This is our free web tutorial index that features a variety of topics related to Java web application development.
🌐
Java Guides
javaguides.net › 2018 › 07 › java-reflection-for-fields.html
Java Reflection for Fields
June 22, 2024 - We can retrieve field information from a class using the Field class in the java.lang.reflect package. Here's an example of how to get field information:
Top answer
1 of 4
16

Something like this...

import java.lang.reflect.Field;

public class Test {
    public static void main(String... args) {
        try {
            Foobar foobar = new Foobar("Peter");
            System.out.println("Name: " + foobar.getName());
            Class<?> clazz = Class.forName("com.csa.mdm.Foobar");
            System.out.println("Class: " + clazz);
            Field field = clazz.getDeclaredField("name");
            field.setAccessible(true);
            String value = (String) field.get(foobar);
            System.out.println("Value: " + value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Foobar {
    private final String name;

    public Foobar(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }
}

Or, you can use the newInstance method of class to get an instance of your object at runtime. You'll still need to set that instance variable first though, otherwise it won't have any value.

E.g.

Class<?> clazz = Class.forName("com.something.Foobar");
Object object = clazz.newInstance();

Or, where it has two parameters in its constructor, String and int for example...

Class<?> clazz = Class.forName("com.something.Foobar");
Constructor<?> constructor = clazz.getConstructor(String.class, int.class);
Object obj = constructor.newInstance("Meaning Of Life", 42);

Or you can interrogate it for its constructors at runtime using clazz.getConstructors()

NB I deliberately omitted the casting of the object created here to the kind expected, as that would defeat the point of the reflection, as you'd already be aware of the class if you do that, which would negate the need for reflection in the first place.

2 of 4
2

You can create instance from class object and that can be used in field get value.

 Class modelClass = Class.forName("com.gati.stackoverflow.EX");
    final Field field = modelClass.getDeclaredField("value");
    field.setAccessible(true);
    Object modelInstance=modelClass.newInstance();
    field.get(modelInstance);
Top answer
1 of 5
66

Assuming a simple case, where your field is public:

List list; // from your method
for(Object x : list) {
    Class<?> clazz = x.getClass();
    Field field = clazz.getField("fieldName"); //Note, this can throw an exception if the field doesn't exist.
    Object fieldValue = field.get(x);
}

But this is pretty ugly, and I left out all of the try-catches, and makes a number of assumptions (public field, reflection available, nice security manager).

If you can change your method to return a List<Foo>, this becomes very easy because the iterator then can give you type information:

List<Foo> list; //From your method
for(Foo foo:list) {
    Object fieldValue = foo.fieldName;
}

Or if you're consuming a Java 1.4 interface where generics aren't available, but you know the type of the objects that should be in the list...

List list;
for(Object x: list) {
   if( x instanceof Foo) {
      Object fieldValue = ((Foo)x).fieldName;
   }
}

No reflection needed :)

2 of 5
6

I strongly recommend using Java generics to specify what type of object is in that List, ie. List<Car>. If you have Cars and Trucks you can use a common superclass/interface like this List<Vehicle>.

However, you can use Spring's ReflectionUtils to make fields accessible, even if they are private like the below runnable example:

List<Object> list = new ArrayList<Object>();

list.add("some value");
list.add(3);

for(Object obj : list)
{
    Class<?> clazz = obj.getClass();

    Field field = org.springframework.util.ReflectionUtils.findField(clazz, "value");
    org.springframework.util.ReflectionUtils.makeAccessible(field);

    System.out.println("value=" + field.get(obj));
}

Running this has an output of:

value=[C@1b67f74
value=3

🌐
Java Code Geeks
javacodegeeks.com › home › core java
How to Retrieve String Fields with Java Reflection - Java Code Geeks
September 2, 2025 - Using ReflectionUtil to get lastName generically. ... Public field value: Tom-Public Private field value: Tom-Private Private field via generic utility: Tom-Private · This confirms that reflection can access both public and private fields, although the approach differs depending on the field’s visibility. This article demonstrated how to use reflection to retrieve String field values from Java objects.
🌐
Java2Blog
java2blog.com › home › core java › reflection › get and set fields using reflection in java
Get and set Fields using reflection in java - Java2Blog
January 11, 2021 - ... Field[] will have all the public fields of the class. If you already know name of the fields you want to access, you can use cl.getField(String fieldName) to get Field object. Field.get() and Field.set() methods can be used get and set value of field respectively.
🌐
Dev.java
dev.java › learn › reflection › fields
Reading and Writing Fields - Dev.java
July 19, 2024 - You can check the page Reflective Access with Open Modules and Open Packages to learn how you can open a module for reflection when you declare it, or some classes from a given package of this module. And you can also check the section The Unnamed Module to learn more about the unnamed module. As you know, primitive types can be automatically boxed to their wrapper types. You need to keep in mind that the Field.get(Object) returns an Object type, and that Field.set(Object,Object) methods takes an Object type.