Skip to content
Ameen Abozaid
Planned lessonMID-009-L04

Custom annotations

  • Lesson 4 of 6
  • Mid-level
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

define and read a runtime annotation.

Planned topics

  • Declare with @interface; elements look like methods.
  • Meta-annotations: @Retention(RUNTIME) so reflection can see it; @Target limits where it applies.
  • Without RUNTIME retention the annotation is invisible at runtime.
  • Read with method.isAnnotationPresent or getAnnotation.
  • Frameworks pair annotations with reflection or proxies.
Draft outline details

Demonstration

java
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
@interface Audited { String action() default ""; }

Check your understanding

What happens with @Retention(SOURCE)?

Show answer

The annotation is dropped before runtime, so reflection cannot see it.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.