Skip to content

Chapter 33 of 37

Reflection

Inspect runtime types and invoke members while respecting access and module boundaries.

38 minutes 10 quick checksBy Subha Prasad
Lesson 33 of 37Course navigation

Lesson content

Read, practise, then check your understanding

Reflection exposes loaded-type metadata through Class, fields, constructors, methods, annotations, and generic descriptors.

Runtime inspection

Class<?> type = Class.forName("com.example.Plugin");
for (Method method : type.getDeclaredMethods()) {
    System.out.println(method.getName());
}

Reflective invocation loses compile-time checking, can wrap failures, and may be slower or restricted by modules. Do not bypass encapsulation casually with accessible overrides. Cache validated metadata when appropriate and whitelist permitted types/members for untrusted configuration. Prefer interfaces, factories, method handles, or generated code when the relationship is known at compile time.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes Class object?
Which Java term matches this description: Runtime metadata representing a loaded type.
Which statement best describes reflection?
Which Java term matches this description: Inspection or invocation of types and members at runtime.
Which statement best describes getDeclaredMethods?
Which Java term matches this description: An API returning methods declared directly by a class.
Which statement best describes Method.invoke?
Which Java term matches this description: Reflective invocation with runtime access and argument checks.
Which statement best describes setAccessible?
Which Java term matches this description: A restricted attempt to suppress normal language access checks.

0 of 10 checks passed

Your progress is saved on this device.