Java Annotations

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Overview

Custom Annotations

Annotation type declarations are similar to normal interface declarations. An at-sign (@) precedes the interface keyword.

Each method declaration defines an element of the annotation type. Method declarations must not have any parameters or a throws clause. Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types.

Methods can have default values.

Annotations themselves may be annotated to indicate where and when they can be used:

@Retention(RetentionPolicy.RUNTIME) // Make this annotation accessible at runtime via reflection.

@Target({ElementType.METHOD}) // This annotation can only be applied to class methods.

The compiler reserves a set of special annotations (including @Deprecated, @Override and @SuppressWarnings) for syntactic purposes.