Fundamentals 13 min read

Introduction to Java Reflection

This article explains Java's reflection mechanism, covering how to obtain Class objects, the java.lang.reflect API, practical code examples, common use cases such as dynamic proxies, JDBC driver loading, Spring bean initialization, and discusses the advantages and performance drawbacks of using reflection.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Introduction to Java Reflection

Reflection is a powerful Java feature that allows programs to inspect and manipulate classes, fields, methods, and constructors at runtime, enabling dynamic access to class information and behavior.

There are three ways to obtain a Class object: ClassName.class, object.getClass(), and Class.forName("full.class.Name").

Each loaded .class file is represented by a Class object in the JVM; this object holds runtime type information (RTTI) which can be used without initializing the class when obtained via .class, but Class.forName triggers initialization.

The reflection API resides in java.lang.reflect and provides three core classes: Field (class attributes), Method (class methods), and Constructor (class constructors). Commonly used methods include getConstructors(), getDeclaredFields(), getMethod(String, Class[]), getDeclaredClasses(), and others for accessing class metadata.

A simple demo defines a User class and uses reflection to obtain its constructors, create an instance, read and modify fields (including private ones via setAccessible(true)), list methods, and invoke both private and public methods:

Class clazz = User.class;
Constructor constructor = clazz.getConstructor(String.class, Integer.class);
Object obj = constructor.newInstance("LiDu", 18);
Field ageField = clazz.getField("age");
Object ageValue = ageField.get(obj);
Method privateMethod = clazz.getDeclaredMethod("privateMethod");
privateMethod.setAccessible(true);
privateMethod.invoke(obj);
Method publicMethod = clazz.getMethod("publicMethod", String.class);
publicMethod.invoke(obj, "LiDu");

Since Java 1.5, generic class literals such as Class<User> userClass = User.class; allow type‑safe instantiation without casting.

Typical real‑world applications of reflection include:

Dynamic proxies: implementing InvocationHandler, creating proxy classes with Proxy.newProxyInstance, and delegating method calls.

JDBC driver loading: Class.forName("com.mysql.jdbc.Driver") to register the driver.

Spring bean creation: parsing XML configuration, loading classes via Class.forName, instantiating beans, and setting properties using Introspector and PropertyDescriptor with reflective method invocation.

Reflection offers flexibility and configurability, allowing code to be driven by external data, but it incurs performance overhead because reflective operations are slower than direct method calls.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaReflectionspringJDBCDynamic Proxy
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.