In order to access private fields, you need to get them from the class's declared fields and then make them accessible:

Field f = obj.getClass().getDeclaredField("stuffIWant"); //NoSuchFieldException
f.setAccessible(true);
Hashtable iWantThis = (Hashtable) f.get(obj); //IllegalAccessException

EDIT: as has been commented by aperkins, both accessing the field, setting it as accessible and retrieving the value can throw Exceptions, although the only checked exceptions you need to be mindful of are commented above.

The NoSuchFieldException would be thrown if you asked for a field by a name which did not correspond to a declared field.

obj.getClass().getDeclaredField("misspelled"); //will throw NoSuchFieldException

The IllegalAccessException would be thrown if the field was not accessible (for example, if it is private and has not been made accessible via missing out the f.setAccessible(true) line.

The RuntimeExceptions which may be thrown are either SecurityExceptions (if the JVM's SecurityManager will not allow you to change a field's accessibility), or IllegalArgumentExceptions, if you try and access the field on an object not of the field's class's type:

f.get("BOB"); //will throw IllegalArgumentException, as String is of the wrong type
Answer from oxbow_lakes on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › reading the value of ‘private’ fields from a different class in java
Reading the Value of 'private' Fields from a Different Class in Java | Baeldung
November 13, 2025 - We can access the private fields that are objects by using the Field#get method. It is to note that the generic get method returns an Object, so we’ll need to cast it to the target type to make use of the value:
🌐
Blogger
javarevisited.blogspot.com › 2012 › 05 › how-to-access-private-field-and-method.html
How to access Private Field and Method Using Reflection in Java? Example Tutorial
In order to access a private field using reflection, you need to know the name of the field than by calling getDeclaredFields(String name) you will get a java.lang.reflect.Field instance representing that field.
🌐
Jenkov
jenkov.com › tutorials › java-reflection › private-fields-and-methods.html
Java Reflection - Private Fields and Methods
April 9, 2018 - Here is a simple example of a class with a private method, and below that the code to access that method via Java Reflection: public class PrivateObject { private String privateString = null; public PrivateObject(String privateString) { this.privateString = privateString; } private String getPrivateString(){ return this.privateString; } } PrivateObject privateObject = new PrivateObject("The Private Value"); Method privateStringMethod = PrivateObject.class.
🌐
Java2Blog
java2blog.com › home › core java › reflection › access private fields and methods using reflection in java
Access private fields and methods using reflection in java - Java2Blog
January 11, 2021 - ... Access private method Can you ... method object which you want to access. Class.getDeclaredField(String fieldName) or Class.getDeclaredFields() can be used to get private fields....
🌐
Programming.Guide
programming.guide › java › accessing-private-fields-of-superclass-through-reflection.html
Java: Accessing private fields of superclass through reflection | Programming.Guide
In Java, difference between default, public, protected, and private ... Executing code in comments?! ... class C { private String privateField = "Hello World"; } class SubC extends C { } class Example { public static void main(String[] args) throws Exception { SubC obj = new SubC(); Field f = SubC.class.getSuperclass().getDeclaredField("privateField"); f.setAccessible(true); String str = (String) f.get(obj); System.out.println(str); // Hello World } }
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › set field value with reflection
Set Field Value With Reflection | Baeldung
January 8, 2024 - Learn how to set values of private fields in Java using the Reflection API.
🌐
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
🌐
W3Schools Blog
w3schools.blog › get-set-private-field-value-in-java
Java reflection get set private field value – W3schools
The get() and set() method are used to get and set public field value in java. Note: We have to turn off the java access check for field modifiers. ... package com.w3spoint; import java.lang.reflect.Field; public class ReflectionTest { public static void main(String args[]){ try { TestClass ...
🌐
Intertech
intertech.com › home › using spring’s reflectionutils to access private fields
Using Spring's ReflectionUtils to Access Private Fields - Intertech
February 13, 2024 - 3. Using reflection, set the field’s accessible flag to true so we can then set the field’s value. The accessible flag toggles reflection access to Java internals, such as fields. This step uses the Spring ReflectionUtils.makeAccessible method.
🌐
Java: How To Do It
javahowtodoit.wordpress.com › 2014 › 08 › 28 › how-to-write-value-to-private-field-using-java-reflection
How to get and set private field using Java reflection | Java: How To Do It
September 12, 2014 - Home › java.lang.reflect › How to get and set private field using Java reflection · Posted on August 28, 2014 by wojr — Leave a comment · // Class declaration class MyClass1 { private String instanceField = "A"; public String getInstanceField() { return instanceField; } } // Given object MyClass1 object = new MyClass1(); // Get field instance Field field = MyClass1.class.getDeclaredField("instanceField"); field.setAccessible(true); // Suppress Java language access checking // Get value String fieldValue = (String) field.get(object); System.out.println(fieldValue); // -> A // Set value field.set(object, "B"); System.out.println(object.getInstanceField()); // -> B ·
🌐
Medium
medium.com › @knoldus › accessing-private-fields-and-methods-using-reflection-27caf3e5bdd4
Accessing private fields and methods using reflection | by Knoldus Inc. | Medium
April 26, 2020 - But as mentioned above, using reflection you can actually access the private methods of a class. And below is the snippet which makes this magic happen. val m = p.getClass.getDeclaredMethod(“greet”) m.setAccessible(true) m.invoke(p) If you run the above sample of code, then you will get the desired output. Hello Harshit ! Now let’s see how did all this happen even via reflection. ... This statement, when executed, will return you the type java.lang.reflect.Method.
🌐
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 - So, we have protected and private fields that won’t be explicitly visible to other classes. Nevertheless, we can use the Class::getDeclaredFields method to get all declared fields.
🌐
Wikibooks
en.wikibooks.org › wiki › Java_Programming › Reflection › Accessing_Private_Features_with_Reflection
Accessing Private Features with Reflection - Wikibooks, open books for an open world
With dp4j [dead link] it is possible to test private members without directly using the Reflection API but simply accessing them as if they were accessible from the testing method; dp4j injects the needed Reflection code at compile-time[2].
Top answer
1 of 8
139

This should demonstrate how to solve it:

import java.lang.reflect.Field;

class Super {
    private int i = 5;
}

public class B extends Super {
    public static void main(String[] args) throws Exception {
        B b = new B();
        Field f = b.getClass().getSuperclass().getDeclaredField("i");
        f.setAccessible(true);
        System.out.println(f.get(b));
    }
}

(Or Class.getDeclaredFields for an array of all fields.)

Output:

5
2 of 8
47

The best approach here is using the Visitor Pattern do find all fields in the class and all super classes and execute a callback action on them.


Implementation

Spring has a nice Utility class ReflectionUtils that does just that: it defines a method to loop over all fields of all super classes with a callback: ReflectionUtils.doWithFields()

Documentation:

Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.

Parameters:
- clazz - the target class to analyze
- fc - the callback to invoke for each field
- ff - the filter that determines the fields to apply the callback to

Sample code:

ReflectionUtils.doWithFields(RoleUnresolvedList.class,
    new FieldCallback(){

        @Override
        public void doWith(final Field field) throws IllegalArgumentException,
            IllegalAccessException{

            System.out.println("Found field " + field + " in type "
                + field.getDeclaringClass());

        }
    },
    new FieldFilter(){

        @Override
        public boolean matches(final Field field){
            final int modifiers = field.getModifiers();
            // no static fields please
            return !Modifier.isStatic(modifiers);
        }
    });

Output:

Found field private transient boolean javax.management.relation.RoleUnresolvedList.typeSafe in type class javax.management.relation.RoleUnresolvedList
Found field private transient boolean javax.management.relation.RoleUnresolvedList.tainted in type class javax.management.relation.RoleUnresolvedList
Found field private transient java.lang.Object[] java.util.ArrayList.elementData in type class java.util.ArrayList
Found field private int java.util.ArrayList.size in type class java.util.ArrayList
Found field protected transient int java.util.AbstractList.modCount in type class java.util.AbstractList

🌐
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.
🌐
In Relation To
in.relation.to › 2017 › 04 › 11 › accessing-private-state-of-java-9-modules
Accessing private state of Java 9 modules - In Relation To
When the @Id annotation is given on a field of an entity, Hibernate will by default directly access fields - as opposed to calling property getters and setters - to read and write the entity’s state. Usually, such fields are private. Accessing them from outside code has never been a problem, though. The Java reflection API allows to make private members accessible and access them subsequently from other classes.
🌐
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: