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 Top answer 1 of 11
207
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);
2 of 11
189
Like answered before, you should use:
Object value = field.get(objectInstance);
Another way, which is sometimes prefered, is calling the getter dynamically. example code:
public static Object runGetter(Field field, BaseValidationObject o)
{
// MZ: Find the correct method
for (Method method : o.getMethods())
{
if ((method.getName().startsWith("get")) && (method.getName().length() == (field.getName().length() + 3)))
{
if (method.getName().toLowerCase().endsWith(field.getName().toLowerCase()))
{
// MZ: Method found, run it
try
{
return method.invoke(o);
}
catch (IllegalAccessException e)
{
Logger.fatal("Could not determine method: " + method.getName());
}
catch (InvocationTargetException e)
{
Logger.fatal("Could not determine method: " + method.getName());
}
}
}
}
return null;
}
Also be aware that when your class inherits from another class, you need to recursively determine the Field. for instance, to fetch all Fields of a given class;
for (Class<?> c = someClass; c != null; c = c.getSuperclass())
{
Field[] fields = c.getDeclaredFields();
for (Field classField : fields)
{
result.add(classField);
}
}
DigitalOcean
digitalocean.com › community › tutorials › java-reflection-example-tutorial
Java Reflection Example Tutorial | DigitalOcean
August 3, 2022 - If the field is final, the set() methods throw java.lang.IllegalAccessException. We know that private fields and methods can’t be accessible outside of the class but using reflection we can get/set the private field value by turning off the java access check for field modifiers.
Videos
Java Reflection - Get and Set field values
20:07
Java Reflection Explained - bɘniɒlqxƎ noiɟɔɘlʇɘЯ ɒvɒᒐ ...
02:14
How to set and get the field values using java Reflection? | ...
00:47
Accessing Fields of an Interface via Reflection #java #shorts #coding ...
18:26
Java Tutorial - Reflection - Constructor and Method Invocation ...
01:26
How to Access Fields of a Class in Java Using Reflection | Reflection ...
GeeksforGeeks
geeksforgeeks.org › java › how-to-access-private-field-and-method-using-reflection-in-java
How to Access Private Field and Method Using Reflection in Java? - GeeksforGeeks
February 5, 2021 - // Access Private Field Using Reflection in Java import java.lang.reflect.Field; // Student class declaration class Student { // private fields private String name; private int age; // Constructor public Student(String name, int age) { this.name = name; this.age = age; } // Getters and setters public String getName() { return name; } public void setName(String name) { this.name = name; } private int getAge() { return age; } public void setAge(int age) { this.age = age; } // Override toString method to get required // output at terminal @Override public String toString() { return "Employee [nam
Coderanch
coderanch.com › t › 378770 › java › reflection-final-static-field
Using reflection to get the value of a final static field (Java in General forum at Coderanch)
January 5, 2006 - In order to do this it needs to call the stored procedure with parameters extracted from an xml file. I've given each parameter an attribute sqltype which should take the name of one of the fields of java.sql.Types and then I'm using reflection to get the actual value of that field.
noredhomepage
noredhomepage.weebly.com › java-reflection-get-field-value-from-object.html
Java reflection get field value from object - noredhomepage
In order to access a private field ... of a public field, you can call the get() method of the Field object, with the object featuring the field value that you'd like to get as the first parameter....
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...
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.lang3.exception.ExceptionUtils; import org.apache.commons.lang3.reflect.FieldUtils; import java.lang.reflect.Field; public class ReflectionUtilsMain { public static void main(String[] args) { Employee employee = new Employee(); employee.setEmpId("1234"); employee.setFirstName("John"); employee.setLastName("Turner"); employee.setDesignation("Manager"); System.out.println(employee); //Now you want to access the First Name from Employee object with Java reflection String firstName = null; String fieldName = "firstName"; for (Class aClass = employee.getClass(); aClass !=
Coderanch
coderanch.com › t › 443479 › java › Property-Field-Reflection
get Property-Field-Name via Reflection (Java in General forum at Coderanch)
April 30, 2009 - The answer is that there doesn't have to be any such field, by any name, so reflection cannot help you. For example, getX() might look like Even if the method returns a variable, it might be named "variableX" or "valueOfX" or anything; or it might be an element of an array, or a value in a HashMap, or a variety of other things. If you need to find this out about one particular class, you can disassemble it with the "javap" tool and look at the bytecode to see how the method is implemented.
Jaketrent
jaketrent.com › post › use-java-reflection-get-field-w-accessor
Use Java Reflection to Get Field w/ Accessor
The only stipulation to this working is that the class follows the JavaBean naming convention standard of "get" + capitalized field name following. Oh, and one more: this is only designed for @Entity's with one @Id field (no composite key). This method goes through all fields, finds the one with the javax.persistence.Id interface annotation, then tries to find a matching accessor method. If it is found, it is executed, the value of the id field is given, finally to be used to create the specially formatted 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.
Google Groups
groups.google.com › g › scala-user › c › DT1Ifw8YKvU
how to get value class fields with scala reflection
April 3, 2013 - It looks like that when using runtime reflection, foo becomes the int value (3) because it extends AnyVal. ... This behavior is expected or are we using the reflection API in a wrong way? I get the same result using SMirror: scala> implicit val mirror = scala.reflect.runtime.currentMirror · mirror: reflect.runtime.universe.Mirror = JavaMirror with scala.t...
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.
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.
Coderanch
coderanch.com › t › 553275 › java › read-string-java-reflection
how to read string value using java reflection [Solved] (Beginning Java forum at Coderanch)
I am setting string(model class string) value in my service class i want to get this in my model class to do compare operation. In model class trying to get using reflection.