Java Reflection API
In this page:
Class<?> clazz = ClassName.class;
Method method = clazz.getMethod("methodName", parameterTypes);
method.invoke(object, arguments);
Accessing Class Objects
Reflection lets you inspect a class's characteristics at runtime -- its fields, superclass references, interfaces, and constructor metadata -- even for classes your code didn't know about at compile time, which is what powers frameworks like Spring and Hibernate under the hood.
Example: Accessing Class Objects
// Define the class Main
public class Main {
// Define the class Sample
static class Sample { int x; }
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Class<?> clazz = Sample.class;
// Print a line to the console
System.out.println(clazz.getName() + " fields: " + clazz.getDeclaredFields().length);
}
}
Login to try C/C++/Java/PHP code in the editor
Accessing and Modifying Private Fields
Using reflection, you can bypass a field's normal access controls (via setAccessible(true)) to inspect or override private variables from outside the class. This is powerful but should be used sparingly outside of frameworks and tests, since it breaks the encapsulation the class author intended.
Example: Accessing and Modifying Private Fields
import java.lang.reflect.Field;
public class Main {
static class Secret { private int value = 42; }
public static void main(String[] args) throws Exception {
Secret s = new Secret();
Field field = Secret.class.getDeclaredField("value");
field.setAccessible(true); // bypasses normal access control
System.out.println(field.getInt(s));
field.setInt(s, 100);
System.out.println(field.getInt(s));
}
}
Login to try C/C++/Java/PHP code in the editor
Calling Methods Dynamically
You can look up and invoke methods dynamically by their string name, passing the target object and runtime arguments to Method.invoke(), which is how frameworks call your annotated methods without knowing their exact signatures at compile time.
Example: Calling Methods Dynamically
import java.lang.reflect.Method;
public class Main {
static class Greeter { String greet(String name) { return "Hello, " + name; } }
public static void main(String[] args) throws Exception {
Method method = Greeter.class.getMethod("greet", String.class);
Object result = method.invoke(new Greeter(), "Riya"); // looked up by name, called dynamically
System.out.println(result);
}
}
Login to try C/C++/Java/PHP code in the editor
Instantiating Classes Dynamically
Constructor reflection lets you instantiate objects at runtime without ever writing an explicit 'new ClassName()' in your code, by looking up a Constructor object and calling newInstance() -- essential for frameworks that create objects based on configuration or annotations rather than hardcoded types.
Example: Instantiating Classes Dynamically
import java.lang.reflect.Constructor;
public class Main {
static class Point { int x; Point(int x) { this.x = x; } }
public static void main(String[] args) throws Exception {
Constructor<Point> ctor = Point.class.getConstructor(int.class);
Point p = ctor.newInstance(5); // no explicit "new Point(5)" in this code
System.out.println(p.x);
}
}
Login to try C/C++/Java/PHP code in the editor
Performance Impact of Reflection
Reflective calls bypass many JIT compiler optimizations that direct method calls benefit from, so they run measurably slower than ordinary programmatic calls. This is usually fine for one-off framework setup work, but worth avoiding inside performance-critical hot loops.
Example: Performance Impact of Reflection
import java.lang.reflect.Method;
public class Main {
static int square(int n) { return n * n; }
public static void main(String[] args) throws Exception {
Method method = Main.class.getDeclaredMethod("square", int.class);
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) method.invoke(null, i); // measurably slower than a direct call
System.out.println("Reflective calls took " + (System.nanoTime() - start) + "ns");
}
}
Login to try C/C++/Java/PHP code in the editor
- Using
getMethodfor a private method, which throws aNoSuchMethodExceptionbecause it only finds public ones; usegetDeclaredMethodandsetAccessible(true). - Calling
method.invoke(obj, ...)with wrong argument types or count, which throws anIllegalArgumentException. - Using reflection where a normal method call would work, which is slower and moves errors from compile time to runtime.
Chapter Quiz — Complete all 19 topics to unlock
0/19 topics done
Complete these topics first:
- Java Reflection API
- Java Annotations Advanced
- Java Garbage Collection
- Java Memory Management
- Java Performance Optimization
- Java Advanced Interview Questions
- Java CompletableFuture
- Java Atomic Classes
- Java Locks & Semaphores
- Java Concurrent Collections
- Java Cryptography Basics
- Java Hashing (MD5, SHA)
- Java SSL & HTTPS
- Java Logging (Log4j/SLF4J)
- Java Serialization Advanced
- Java Interview Questions Advanced
- Java Connection Pooling
- Java Test Driven Development
- Java Integration Testing