Understanding Java Reflection: Concepts, Common Methods, and Example Exercises
This article explains Java's reflection mechanism, describing how Class objects represent runtime types, how to obtain them, and demonstrating common reflective operations such as inspecting methods, accessing fields, invoking constructors, handling arrays, and solving typical exercises with complete code examples.
Reflection maps Java components to corresponding Class objects, allowing runtime inspection and manipulation of classes, methods, and fields.
Class objects are created by the JVM; they have no public constructors and represent classes, interfaces, arrays, primitives, and void.
Common ways to obtain a Class object include using obj.getClass() , the class literal ClassName.class , and Class.forName("full.package.ClassName") .
Example code demonstrates listing methods of java.util.Stack via Class.forName and getDeclaredMethods() :
import java.lang.reflect.*;
public class DumpMethods {
public static void main(String args[]) {
try {
Class c = Class.forName("java.util.Stack");
Method m[] = c.getDeclaredMethods();
for (int i = 0; i < m.length; i++)
System.out.println(m[i].toString());
} catch (Throwable e) {
System.err.println(e);
}
}
}Utility methods such as isPrimitive() , isArray() , and constructors retrieval ( getConstructor , getConstructors ) are shown.
Field manipulation is illustrated, including accessing public fields directly and private fields via getDeclaredField with setAccessible(true) :
ReflectPointer rp1 = new ReflectPointer(3,4);
Field fieldx = rp1.getClass().getField("x");
System.out.println(fieldx.get(rp1));
Field fieldy = rp1.getClass().getDeclaredField("y");
fieldy.setAccessible(true);
System.out.println(fieldy.get(rp1));Typical exercises include changing all String fields' characters, invoking a class’s main method reflectively, and simulating the instanceof operator using Class.isInstance :
Method m = Class.forName(className).getMethod("main", String[].class);
m.invoke(null, new Object[]{new String[]{"111","222","333"}});
Class cls = Class.forName("S");
System.out.println(cls.isInstance(new Integer(37)));
System.out.println(cls.isInstance(new S()));The Method class is used to invoke methods such as String.charAt on objects:
Method mtCharAt = String.class.getMethod("charAt", int.class);
Object ch = mtCharAt.invoke(str, 1);
System.out.println(ch);Array reflection is covered, showing that arrays of the same type share the same Class object, and demonstrating differences between primitive and wrapper class representations:
int[] a = new int[3];
int[] b = new int[]{4,5,5};
System.out.println(a.getClass() == b.getClass()); // true
System.out.println(a.getClass().getName()); // [IThe article concludes that reflection underpins many frameworks (e.g., Spring) and enables dynamic, loosely coupled Java applications.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.