Class Inspection with Reflection
Retrieve Class objects, inspect fields, methods, constructors, and annotations at runtime.
Class Inspection with Reflection is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Reflection?
Reflection allows a Java program to inspect and manipulate its own structure at runtime — reading class names, fields, methods, constructors, and annotations without knowing them at compile time.
Obtaining a Class Object
Get a Class<?> object via the literal, getClass(), or Class.forName().
Class<String> c1 = String.class; // literal
Class<?> c2 = "hello".getClass(); // from instance
Class<?> c3 = Class.forName("java.util.ArrayList"); // by nameInspecting Class Metadata
Retrieve the class name, package, superclass, and implemented interfaces from the Class object.
Class<?> c = ArrayList.class;
System.out.println(c.getName()); // java.util.ArrayList
System.out.println(c.getSimpleName()); // ArrayList
System.out.println(c.getSuperclass()); // class java.util.AbstractList
for (Class<?> iface : c.getInterfaces()) System.out.println(iface.getName());Listing Declared Fields
getDeclaredFields() returns all fields declared in the class (including private). getFields() returns only public fields including inherited ones.
for (Field f : User.class.getDeclaredFields()) {
System.out.printf("%-10s %s%n", f.getType().getSimpleName(), f.getName());
}Listing Methods
getDeclaredMethods() lists all methods (any access). Use getMethods() for public methods including inherited ones.
for (Method m : String.class.getDeclaredMethods()) {
System.out.println(m.getName() + " -> " + m.getReturnType().getSimpleName());
}Inspecting Method Parameters
Retrieve parameter types, names (requires -parameters compiler flag), and annotations.
Method m = UserService.class.getDeclaredMethod("findById", Long.class);
for (Parameter p : m.getParameters()) {
System.out.println(p.getType().getSimpleName() + " " + p.getName());
}Listing Constructors
Inspect constructors just like methods. getDeclaredConstructors() includes private ones used in singletons or builders.
for (Constructor<?> c : User.class.getDeclaredConstructors()) {
System.out.println(Arrays.toString(c.getParameterTypes()));
}Reading Annotations
Retrieve annotations on the class, method, or field. Check for presence with isAnnotationPresent and get the value with getAnnotation.
@RestController
@RequestMapping("/users")
public class UserController {}
// Inspection:
RequestMapping rm = UserController.class.getAnnotation(RequestMapping.class);
if (rm != null) System.out.println("Path: " + Arrays.toString(rm.value()));Checking Modifiers
Use Modifier.isPublic(), isStatic(), isFinal(), etc., on field/method/class modifiers to filter by visibility.
for (Field f : User.class.getDeclaredFields()) {
int mod = f.getModifiers();
if (Modifier.isPrivate(mod)) System.out.println("private: " + f.getName());
}isAssignableFrom and instanceof Check
Use Class.isAssignableFrom() to check type compatibility programmatically — equivalent to instanceof but with class objects known only at runtime.
Class<?> parent = Collection.class;
Class<?> child = ArrayList.class;
System.out.println(parent.isAssignableFrom(child)); // true
System.out.println(child.isAssignableFrom(parent)); // falsePerformance Caveat
Reflection is 10–50× slower than direct code for simple field/method access. Cache Field and Method objects; call setAccessible(true) once and reuse.
private static final Field NAME_FIELD;
static {
try { NAME_FIELD = User.class.getDeclaredField("name"); NAME_FIELD.setAccessible(true); }
catch (NoSuchFieldException e) { throw new RuntimeException(e); }
}Quick Check
Which method returns private fields of a class (but not inherited)?
Recap
Obtain Class via literals or forName(). Use getDeclaredFields/Methods/Constructors for all-access inspection. Read annotations with getAnnotation. Cache reflective objects for performance.
Frequently asked questions
Is the “Class Inspection with Reflection” lesson free?
Yes — the full text of “Class Inspection with Reflection” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Class Inspection with Reflection”?
Retrieve Class objects, inspect fields, methods, constructors, and annotations at runtime. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Class Inspection with Reflection” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Class Inspection with Reflection
- Invoking Methods and Accessing Fields
- Dynamic Proxies with InvocationHandler
- Reflection Performance and Alternatives