Java learning pathSection 18

Reflection

Explore runtime inspection and dynamic method and field access using Java reflection.

1

Reflection API

The reflection API lets Java code inspect classes, methods, fields, constructors, and annotations at runtime.

This is widely used by frameworks, dependency injection systems, and tooling.

Class metadata feeds the reflection API, which branches to Method, Field, and Constructor objects for dynamic invocation, field access, and reflective instantiation.
Reflection API
2

Class Class

The Class object represents a runtime type and provides metadata and access to the class structure.

It is the fundamental entry point for reflective inspection in Java.

3

Method Class

Method objects encapsulate method metadata and support invocation at runtime.

They allow code to discover and call methods dynamically.

4

Field Class

Field objects represent class or instance variables and allow reading or writing them reflectively.

This is useful in frameworks and advanced inspection tools.

5

Constructor Class

Constructor objects represent class constructors and support reflective instantiation.

They enable creation of objects without direct compile-time knowledge of a specific type.

6

Dynamic Invocation

Dynamic invocation uses reflection to call methods or constructors without compile-time linkage.

This provides flexibility but requires careful validation and error handling.

7

Dynamic Proxy

Dynamic proxies generate implementations of interfaces at runtime, allowing interception and delegation patterns.

This is a common technique in frameworks such as AOP and proxy-based instrumentation.

8

Reflection Performance

Reflection is usually slower than direct method calls because it performs runtime checks and dynamic resolution.

This is one reason frameworks should use reflection carefully and cache metadata where possible.