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)
Because such access usually violates the design intentions of the class, it should be used with the utmost discretion. 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
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 › retrieve fields from a java class using reflection
Retrieve Fields from a Java Class Using Reflection | Baeldung
January 4, 2026 - Of course, we could use the getDeclaredFields() method on both Person and Employee classes and merge their results into a single array. But what if we don’t want to specify the superclass explicitly?
🌐
Jenkov
jenkov.com › tutorials › java-reflection › fields.html
Java Reflection - Fields
April 21, 2016 - Class aClass = MyObject.class Field field = aClass.getField("someField"); MyObject objectInstance = new MyObject(); Object value = field.get(objectInstance); field.set(objetInstance, value); The objectInstance parameter passed to the get and ...
🌐
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.
🌐
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 - But, it “upgraded” to a permanent feature in Java 16. The RecordComponent class provides information about a component of a record class. Moreover, the Class class provides the getRecordComponents() method to return all components of a record class if the class object is a Record instance. It’s worth noting that the components in the returned array are in the same order declared in the record. Next, let’s see how to use RecordComponent together with the reflection API to get all fields from our Player record class and the corresponding values of the ERIC instance:
Find elsewhere
🌐
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.
🌐
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) { String stringValue = (String) fieldValue; // do something with the string value } ...
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.
🌐
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.
🌐
Hubberspot
hubberspot.com › 2012 › 09 › how-to-get-field-set-and-get-its-value.html
How to get field, set and get its value by using Field class in Java Reflection API ? | Learn Java by Examples
Program to demonstrate working of Field class in Java Reflection API for getting field, setting and getting its value · package com.hubberspot.example; import java.lang.reflect.Field; class Customer { public String firstname; public String lastname; public String email; public static int counter; } public class FieldDemo { public static void main(String[] args) { // Create a test object.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › reflect › Field.html
Field (Java Platform SE 8 )
2 weeks ago - Gets the value of a static or instance boolean field. ... IllegalAccessException - if this Field object is enforcing Java language access control and the underlying field is inaccessible.
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › reflect › object › get.html
Getting Field Values
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 printHeight(Rectangle r) { Field heightField; Integer heightValue; Class c = r.getClass(); try { heightField = c.getFie
🌐
Java Spring Works
javaspringworks.com › home › all you need to know about java reflection – field class
All you need to know about Java Reflection – Field class - Java Spring Works
April 13, 2025 - We can also get the values of a Field of a specific object. Let’s see an example similar to our known program: package org.example.reflection; import java.lang.reflect.Field; public class Main { public static void main(String[] args) throws IllegalAccessException { Movie movie = new Movie("The Shawshank Redemption", 1994, false, Category.DRAMA, 10.0); printDeclaredFields(Movie.class, movie); } public static <T> void printDeclaredFields(Class<? extends T> clazz, T instance) throws IllegalAccessException { for (Field field : clazz.getDeclaredFields()) { System.out.println(String.format("Field
🌐
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.
🌐
Bala's Blog
dkbalachandar.wordpress.com › 2017 › 03 › 08 › how-to-use-reflectionutils-to-retrieve-the-field-value
How to use ReflectionUtils to retrieve the field value | Bala's Blog
March 8, 2017 - import org.apache.commons.lang... Employee object with Java reflection String firstName = null; String fieldName = "firstName"; for (Class aClass = employee.getClass(); aClass !=...