Learn Java
import com.learnjava.oopadvanced.Reflection;

Reflection in Java

@Concept

Reflection is the ability for a running Java program to examine and manipulate its own classes, methods, and fields at runtime, even ones it has no compile-time reference to — given nothing but a String naming a class, reflection can inspect what fields and methods that class has, create an instance of it, and call its methods, all dynamically. Class.forName("com.example.MyClass") loads a class by name at runtime, and from the resulting Class object, methods like getDeclaredFields() and getDeclaredMethods() reveal everything about that class's structure that would normally only be visible by reading its source code directly.

Reflection is genuinely powerful but comes with real costs: it bypasses normal compile-time type checking, so mistakes surface as runtime exceptions instead, it's noticeably slower than direct method calls, and it can even reach past access modifiers to touch private fields and methods. That's exactly why frameworks needing genuine flexibility — dependency injection, serialization libraries, testing tools — rely on it heavily, while ordinary application code almost never needs to reach for it directly.

@HowItWorks
1

Loading a class by name

Class.forName("com.example.MyClass") loads a class dynamically at runtime, given just a String.

2

Inspecting structure

getDeclaredFields() and getDeclaredMethods() reveal a class's fields and methods without needing its source code.

3

Creating instances dynamically

Reflection can construct a new object of a class discovered only at runtime, with no compile-time reference to it.

4

Calling methods dynamically

A Method object obtained through reflection can be invoked on a target object, even without knowing that method at compile time.

5

Real costs

Reflection bypasses compile-time type checking, runs slower than direct calls, and can reach past access modifiers meant to restrict visibility.

@Example
public class ReflectionDemo {
static class Sample {
int value;
String label;
}
 
public static void main(String[] args) throws Exception {
Class<?> clazz = Class.forName("ReflectionDemo$Sample");
System.out.println("Class name: " + clazz.getSimpleName());
System.out.println("Field count: " + clazz.getDeclaredFields().length);
}
}
Output
Class name: Sample
Field count: 2
1Class.forName loads Sample dynamically at runtime using its binary name, ReflectionDemo$Sample for a nested class — the way a framework would load a class it only knows by name from a config file.
2clazz.getSimpleName() returns just "Sample", the class's own name without its enclosing class prefix.
3clazz.getDeclaredFields() reflects Sample's actual two fields, value and label, discovered through the Class object rather than compile-time code.
4Both println calls run once each, reporting what reflection extracted about the class's structure.
@UseCases
Building a plugin system that loads and instantiates classes named only in a configuration file
Writing a dependency-injection or serialization framework that inspects arbitrary classes generically
Understanding why reflection-based code fails at runtime instead of at compile time when something's wrong
Recognizing when ordinary application code should avoid reflection in favor of direct, compile-time-checked code

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.