Nearby lessons
106 of 125Java - Reflection API
- What reflection means
- How to inspect a class at run time
- Creating objects and calling methods with reflection
- Why reflection powers every big framework
Reflection API is a core concept of the Java language. This lesson explains What is Reflection?, Normal Code vs Reflective Code and The Gate — Class Object with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
What is Reflection?
Reflection is the ability of a Java program to look at itself while it is running — to find out the methods, fields and constructors of a class, and even to use them, without knowing the class details at compile time.
Think of a mirror: the program holds a mirror to its own classes and examines them at run time. All of this lives in the java.lang.reflect package.
Normal code says: I know exactly which class I am using. Reflective code says: Let me first find out what this class has, then I will decide what to do.
Normal Code vs Reflective Code
| Point | Normal (direct) code | Reflection code |
|---|---|---|
| What you know | The class name and types at compile time | The class is known only at run time |
| How objects are made | new ClassName() | Class.forName(...).newInstance() |
| Compiler checks? | Yes — mistakes found at compile time | No — mistakes appear at run time |
| Speed | Fast | Slower |
| When used | Everyday programming | Frameworks, tools, testing |
The Gate — Class Object
Every loaded class has a Class object that holds its full description. We get it in three ways:
Inspecting a Class
Creating an Object and Calling a Method
With reflection we can create an object even when we know the class only as a string, and call its methods using invoke().
Private Members — The Power (and Danger)
Reflection can even access private fields and methods by calling setAccessible(true). This is powerful but must be used carefully — it breaks encapsulation.
Why Reflection Matters — The Framework Secret
Reflection is the engine behind every big framework. Here is the insight:
- JUnit uses reflection to find and run all methods starting with @Test.
- Spring uses reflection to create objects (beans) from classes and to inject dependencies.
- Hibernate uses reflection to map your class fields to database columns.
- Annotations (Chapter 20) are processed at run time using reflection.
Performance Note
Reflection is slower than normal code and the compiler cannot check it (so errors appear at run time). Real applications therefore use reflection at startup time (to build objects), then switch to normal calls for the rest of the program.
Reading Fields, Methods and Modifiers
Beyond listing methods, reflection can inspect a class's fields, their modifiers, and a method's parameters — this is exactly what frameworks do to understand your class:
getDeclaredFields() gives all fields (even private), Modifier.toString(...) turns access flags into words like public final, and getParameterTypes() lists a method's parameters. These three calls are the heart of framework inspection.
- Reflection lets a program inspect and use its classes at run time.
- The Class object (Class.forName, .class, getClass()) is the gateway.
- We can list methods/fields, create objects, and call methods with invoke().
- setAccessible(true) reaches private members — powerful but risky.
- Reflection powers frameworks: JUnit, Spring, Hibernate, annotation processing.
- It is slower and unchecked, so use it at startup, not in hot loops.