Understanding Java Reflection: Principles, Usage, and Code Examples
This article explains Java's reflection mechanism, its underlying principles involving the Class object, how to obtain Class instances, provides code examples, outlines common use cases such as dynamic loading, frameworks, and middleware, and concludes with a promotional note for an architecture learning collection.
Java Reflection Mechanism Overview
Java interviewers often ask about the reflection mechanism, including what it is, why it is used, its underlying principles, and typical application scenarios.
What Is Java Reflection?
Reflection allows a program, at runtime, to discover the full set of fields, methods, annotations, and other metadata of any class or object, and to invoke methods or access fields dynamically.
Why Use Reflection?
Obtain class name, package information, all fields, methods, annotations, and the class loader.
Read and modify an object's fields.
Invoke any method on an object.
Determine the class to which an object belongs.
Instantiate objects of any class.
Enable dynamic assembly, reduce coupling, and implement dynamic proxies.
Underlying Principle
Reflection operates on the java.lang.Class object, so understanding the Class object is essential. When a class is loaded, the JVM creates a Class instance that represents the byte‑code metadata of that class.
Example of creating an object:
MikeChen mikechen = new MikeChen();During execution, the JVM loads the compiled .class file via a ClassLoader , creates a Class object, and stores it in the method area while the instance lives on the heap.
How to Obtain a Class Object
Call getClass() on an instance.
Use the ClassName.class syntax.
Invoke Class.forName("full.class.Name") .
Use ClassLoader.getSystemClassLoader().loadClass("full.class.Name") .
Code example demonstrating these methods:
package com.mikechen.reflection;
import com.mikechen.model.Person;
public class ReflectionTest {
public static void main(String[] args) throws Exception {
// 1. instance.getClass()
Person person = new Person();
Class clz1 = person.getClass();
System.out.println(clz1);
// 2. ClassName.class
System.out.println(Person.class);
// 3. Class.forName()
Class clz2 = Class.forName("com.mikechen.reflection.Person");
System.out.println(clz2);
}
}The three approaches all retrieve the Class object of Person , but they differ: the first requires an instance, the second needs the class to be imported, and the third only needs the fully‑qualified class name as a string.
Dynamic Loading and Use Cases
Reflection enables loading classes only when needed, which is essential for middleware and frameworks. Common scenarios include:
JDBC driver loading with Class.forName(driverClass) .
Spring MVC invoking controller methods via reflection and handling requests with dynamic proxies.
Spring IoC container creating beans and performing dependency injection.
RMI deserialization and remote method invocation.
Dubbo RPC dynamic proxy usage.
Conclusion and Additional Resources
The article provides a comprehensive overview of Java reflection, its implementation details, and practical applications. At the end, the author promotes a collection titled "Alibaba Architect Advancement from 0 to 1," which covers distributed systems, middleware, microservices, large‑scale website architecture, and other core skills for senior engineers.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.