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
🌐
Jenkov
jenkov.com › tutorials › java-reflection › private-fields-and-methods.html
Java Reflection - Private Fields and Methods
April 9, 2018 - Note: There has been a lot of talk ... might change in a future Java version. To access a private field you will need to call the Class.getDeclaredField(String name) or Class.getDeclaredFields() method....
🌐
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.
🌐
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 - Java Collection · Last Updated : 5 Feb, 2021 · If we want to access Private Field and method using Reflection we just need to call setAccessible(true) on the field or method object which you want to access.
🌐
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. So the first statement - p.getClass.getDeclaredMethod(“greet”) This statement, when executed, will return you the type java.lang.reflect.Method.
🌐
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....
Find elsewhere
🌐
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 } }
🌐
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.
🌐
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.
🌐
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].
🌐
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 - // 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
🌐
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.
🌐
Programiz
programiz.com › java-programming › examples › access-private-members
Java Program to Access private members of a class
import java.lang.reflect.*; class Test { // private variables private String name; // private method private void display() { System.out.println("The name is " + name); } } class Main { public static void main(String[] args) { try { // create an object of Test Test test = new Test(); // create an object of the class named Class Class obj = test.getClass(); // access the private variable Field field = obj.getDeclaredField("name"); // make private field accessible field.setAccessible(true); // set value of field field.set(test, "Programiz"); // get value of field // and convert it in string Stri