Loading published curriculum…

04 · Java language in depth / LESSON 16 OF 102

Annotations and reflection before Spring

Annotations attach metadata; a compiler, tool, or runtime reader must give that metadata meaning.

Before this lesson: Optional, time, and exact money

Understand the topic

@Target limits where an annotation can be used. @Retention controls whether it survives source compilation and whether reflection can read it. RUNTIME retention is necessary for runtime discovery; writing an annotation alone does not execute business behavior.

Reflection inspects classes and methods at runtime but can encounter module-access restrictions and wraps invocation errors. Prefer ordinary typed calls for application behavior. Frameworks use metadata to register objects, map requests, and apply interceptors.

EXAMPLE · JAVA

Main.java

import java.lang.annotation.*;
public class Main {
  @Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.TYPE)
  @interface Label { String value(); }
  @Label("tasks") static class Task {}
  public static void main(String[] args) {
    System.out.println(Task.class.getAnnotation(Label.class).value());
  }
}

What to expect

Prints tasks. Changing retention to SOURCE makes runtime lookup return null.

YOUR TURN

Put it into practice

  1. 01Define an annotation marking an exportable field.
  2. 02Inspect its metadata with reflection and compare SOURCE versus RUNTIME retention.
  3. 03Verify: You can explain separately who stores the annotation and who reads it to perform work.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

Annotations attach metadata; a compiler, tool, or runtime reader must give that metadata meaning.

  1. 01@Target limits where an annotation can be used. @Retention controls whether it survives source compilation and whether reflection can read it. RUNTIME retention is necessary for runtime discovery; writing an annotation alone does not execute business behavior.
  2. 02Reflection inspects classes and methods at runtime but can encounter module-access restrictions and wraps invocation errors. Prefer ordinary typed calls for application behavior. Frameworks use metadata to register objects, map requests, and apply interceptors.
  3. 03Trace the example: Prints tasks. Changing retention to SOURCE makes runtime lookup return null.

A mistake worth avoiding

getAnnotation returns null even though the source contains the annotation.

DEBUGGING ROOM

Something broke. Where do you look?

getAnnotation returns null even though the source contains the annotation.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Does a custom annotation automatically run logic?

Choose one answer

BUILD IT YOURSELF

Define an annotation marking an exportable field.

  1. 1.Define an annotation marking an exportable field.
  2. 2.Inspect its metadata with reflection and compare SOURCE versus RUNTIME retention.
  3. 3.Reproduce this failure, then repair it: getAnnotation returns null even though the source contains the annotation.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Does a custom annotation automatically run logic?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

You can explain separately who stores the annotation and who reads it to perform work.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references