Skip to content
Reflection — Explained with Examples

Reflection — Explained with Examples

DodaTech Updated Jun 15, 2026 1 min read

Reflection is the ability of a program to examine, introspect, and even modify its own structure and behavior at runtime. Reflective capabilities typically include: enumerating class members, calling methods by name (string-based invocation), accessing and modifying fields dynamically, and creating new instances of types discovered at runtime.

Reflection enables powerful patterns like dependency injection containers, ORMs (object-relational mapping), serialization libraries, and testing frameworks. However, reflection often trades type safety and performance for flexibility. Languages with strong reflection include Java (java.lang.reflect), C# (System.Reflection), Python (first-class everything), and JavaScript (dynamic property access). Reflection is a form of Metaprogramming.

Real-world analogy. Reflection is like looking at yourself in a mirror while also having a medical X-ray machine. You can see your external appearance (introspect class members), check your bone structure (examine type hierarchy), and even perform surgery on yourself (modify behavior at runtime). Normally you’d use a doctor (the compiler), but reflection lets you be your own diagnostician.

Example (Java reflection):

Class<?> cls = Class.forName("com.example.User");
Object user = cls.getDeclaredConstructor().newInstance();
Method setName = cls.getMethod("setName", String.class);
setName.invoke(user, "Alice");
Field nameField = cls.getDeclaredField("name");
nameField.setAccessible(true);
System.out.println(nameField.get(user));  // "Alice"

Related terms: Metaprogramming, AOP, Dependency Injection, Dynamic Typing, Type System

Related tutorial: Reflection in Programming

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro