Mastering Java Reflection: What It Is, How It Works, and Real-World Uses
Java reflection lets programs inspect and manipulate classes, methods, and fields at runtime, enabling dynamic object creation, method invocation, and flexible framework design; this guide explains its definition, benefits, underlying mechanics, practical usage with code examples, and common scenarios such as JDBC driver loading and Spring IoC.
Reflection is extremely powerful and useful; most modern frameworks (Spring, MyBatis, RocketMQ, etc.) rely on it, making reflection a cornerstone of framework design.
Therefore, mastering reflection is essential on the path to advanced Java development.
Key questions to understand:
1. What is reflection? 2. What are its uses? 3. How does it work internally? 4. How to use reflection?
What is reflection?
Reflection is a feature of the Java language that allows a program to examine its own structure and manipulate its members at runtime (not at compile time).
At runtime, for any class you can discover all its fields and methods, and for any object you can invoke any method or access any field; this dynamic capability is called Java's reflection mechanism.
In one sentence: reflection lets you discover a class’s full structure at runtime and invoke its members dynamically.
Why use reflection?
Java reflection is powerful and useful, for example:
Obtain a class’s name, package, all fields, methods, annotations, type, class loader, etc.
Read and modify an object’s fields.
Invoke any method on an object.
Determine the class of any object.
Instantiate an object of any class.
Implement dynamic assembly, reduce coupling, create dynamic proxies, etc.
How to use reflection?
There are two common ways to create an object via reflection:
Using the Class object's newInstance() method.
Using a Constructor object's newInstance() method.
Method 1: Using Class.newInstance()
Class clz = Class.forName("com.mikechen.reflection.JiaGou");
JiaGou jg = (JiaGou) clz.newInstance();Method 2: Using Constructor.newInstance()
Class clz = Class.forName("com.mikechen.reflection.JiaGou");
Constructor constructor = clz.getConstructor();
JiaGou jg = (JiaGou) constructor.newInstance();Using a Constructor allows you to choose a specific constructor, while Class can only use the default no‑arg constructor. The following example shows invoking a constructor with parameters.
Class clz = Class.forName("com.mikechen.reflection.JiaGou");
Constructor constructor = clz.getConstructor(String.class);
JiaGou jg = (JiaGou) constructor.newInstance("mikechen的互联网架构");Accessing class members via reflection
1. Getting fields
Field[] fields = cls.getDeclaredFields();2. Getting methods
Method[] methods = cls.getDeclaredMethods();3. Getting constructors
Constructor[] constructors = cls.getDeclaredConstructors();With reflection you can retrieve a class’s complete structure at runtime.
How reflection works internally
The overall process is:
After compiling a Java project, each .java file becomes a .class file.
At runtime, the ClassLoader loads these class files into the JVM, creating a Class object for each loaded class.
Through the Class object you can obtain Field, Method, and Constructor objects.
When we normally create objects with new, the JVM uses the already‑generated Class objects behind the scenes.
Reflection enables dynamic loading of classes that may not be needed at startup, reducing initial load time and increasing flexibility.
Typical application scenarios
Example: a project may need to switch between MySQL and Oracle drivers at runtime. Using Class.forName() you can load the appropriate driver class dynamically.
Spring’s IoC container loads beans based on configuration, and Spring AOP’s dynamic proxies rely on reflection.
Other frameworks such as MyBatis, Dubbo, RocketMQ, etc., also make extensive use of reflection.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
