Sobes.tech
Junior — Middle

What functions and capabilities does Reflection API provide in programming language?

sobes.tech AI

Answer from AI

The Reflection API in Java allows a program to examine and modify the structure and behavior of objects at runtime. Main capabilities:

  • Obtaining information about classes, interfaces, methods, fields, constructors.
  • Dynamically creating new instances of classes.
  • Invoking methods and accessing fields of objects, even if they are private (using setAccessible).
  • Annotation analysis.

Example usage:

Class<?> clazz = Class.forName("java.util.ArrayList");
Object instance = clazz.getDeclaredConstructor().newInstance();
Method addMethod = clazz.getMethod("add", Object.class);
addMethod.invoke(instance, "Hello");
System.out.println(instance);

Reflection is useful for frameworks, serialization, testing, and dynamic programming.

What functions and capabilities does Reflection API… - sobes.tech