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
🌐
Oracle
docs.oracle.com › javase › tutorial › reflect › member › fieldValues.html
Getting and Setting Field Values (The Java™ Tutorials > The Reflection API > Members)
The Book class illustrates how to set the values for long, array, and enum field types. Methods for getting and setting other primitive types are described in Field. import java.lang.reflect.Field; import java.util.Arrays; import static java.lang.System.out; enum Tweedle { DEE, DUM } public class Book { public long chapters = 0; public String[] characters = { "Alice", "White Rabbit" }; public Tweedle twin = Tweedle.DEE; public static void main(String...
🌐
TutorialsPoint
tutorialspoint.com › javareflect › javareflect_field_get.htm
java.lang.reflect.Field.get() Method Example
The java.lang.reflect.Field.get(Object obj) method returns the value of the field represented by this Field, on the specified object. The value is automatically wrapped in an object if it has a primitive type.
🌐
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 - Nevertheless, we can use the Class::getDeclaredFields method to get all declared fields. The getDeclaredFields() method returns an array of Field objects alongside all the metadata of the fields:
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

🌐
Jenkov
jenkov.com › tutorials › java-reflection › fields.html
Java Reflection - Fields
April 21, 2016 - Field field = aClass.getField("someField"); Object fieldType = field.getType(); Once you have obtained a Field reference you can get and set its values using the Field.get() and Field.set()methods, like this:
🌐
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.
🌐
W3Docs
w3docs.com › java
Reflection generic get field value
class MyClass<T> { public T field; } // ... MyClass<String> myObject = new MyClass<>(); myObject.field = "value"; Field field = MyClass.class.getDeclaredField("field"); Object fieldValue = field.get(myObject); if (fieldValue instanceof String) ...
Find elsewhere
🌐
Java-tips
java-tips.org › home › java.lang › how to retrieve field values using java reflection
How to retrieve field values using Java reflection - Java Tips
First, a Field object is retrieved for each field of interest by calling the getField(String name) method of the java.lang.Class class, which takes the field name as argument; then, getInt() is called on each Field object to get the int value of the corresponding field. import java.awt.*; import ...
🌐
Dev.java
dev.java › learn › reflection › fields
Reading and Writing Fields - Dev.java
July 19, 2024 - 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.
🌐
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.
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);
🌐
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.
Top answer
1 of 3
147

You can use Class#getDeclaredFields() to get all declared fields of the class. You can use Field#get() to get the value.

In short:

Object someObject = getItSomehow();
for (Field field : someObject.getClass().getDeclaredFields()) {
    field.setAccessible(true); // You might want to set modifier to public first.
    Object value = field.get(someObject); 
    if (value != null) {
        System.out.println(field.getName() + "=" + value);
    }
}

To learn more about reflection, check the Oracle tutorial on the subject.

That said, if that VO is a fullworthy Javabean, then the fields do not necessarily represent real properties of a VO. You would rather like to determine the public methods starting with get or is and then invoke it to grab the real property values.

for (Method method : someObject.getClass().getDeclaredMethods()) {
    if (Modifier.isPublic(method.getModifiers())
        && method.getParameterTypes().length == 0
        && method.getReturnType() != void.class
        && (method.getName().startsWith("get") || method.getName().startsWith("is"))
    ) {
        Object value = method.invoke(someObject);
        if (value != null) {
            System.out.println(method.getName() + "=" + value);
        }
    }
}

That in turn said, there may be more elegant ways to solve your actual problem. If you elaborate a bit more about the functional requirement for which you think that this is the right solution, then we may be able to suggest the right solution. There are many, many tools available to massage javabeans. There's even a built-in one provided by Java SE in the java.beans package.

BeanInfo beanInfo = Introspector.getBeanInfo(someObject.getClass());
for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
    Method getter = property.getReadMethod(); 
    if (getter != null) {
        Object value = getter.invoke(someObject);
        if (value != null) {
            System.out.println(property.getName() + "=" + value);
        }
    }
}
2 of 3
12

Here's a quick and dirty method that does what you want in a generic way. You'll need to add exception handling and you'll probably want to cache the BeanInfo types in a weakhashmap.

public Map<String, Object> getNonNullProperties(final Object thingy) {
    final Map<String, Object> nonNullProperties = new TreeMap<String, Object>();
    try {
        final BeanInfo beanInfo = Introspector.getBeanInfo(thingy
                .getClass());
        for (final PropertyDescriptor descriptor : beanInfo
                .getPropertyDescriptors()) {
            try {
                final Object propertyValue = descriptor.getReadMethod()
                        .invoke(thingy);
                if (propertyValue != null) {
                    nonNullProperties.put(descriptor.getName(),
                            propertyValue);
                }
            } catch (final IllegalArgumentException e) {
                // handle this please
            } catch (final IllegalAccessException e) {
                // and this also
            } catch (final InvocationTargetException e) {
                // and this, too
            }
        }
    } catch (final IntrospectionException e) {
        // do something sensible here
    }
    return nonNullProperties;
}

See these references:

  • BeanInfo (JavaDoc)
  • Introspector.getBeanInfo(class) (JavaDoc)
  • Introspection (Sun Java Tutorial)
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › reflect › object › get.html
Getting Field Values
Because the height is a primitive type (int), the object returned by the get method is a wrapper object (Integer). In the sample program, the name of the height field is known at compile time. However, in a development tool such as a GUI builder, the field name might not be known until runtime. To find out what fields belong to a class, you can use the techniques described in the section Identifying Class Fields. ... import java.lang.reflect.*; import java.awt.*; class SampleGet { public static void main(String[] args) { Rectangle r = new Rectangle(100, 325); printHeight(r); } static void prin
🌐
Netjstech
netjstech.com › 2017 › 07 › reflection-in-java-field.html
Reflection in Java - Getting Field Information | Tech Tutorials
December 23, 2021 - How to get information about the fields of a class using reflection API in Java. Get field's type, field’s modifier and setting and getting values of a field using reflection.
🌐
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...