Built-In Annotations

Java defines many built-in annotations. Most are specialized, but seven are general purpose.


@Retention
@Documented
@Target 
@Inherited
@Override
@Deprecated 
@SuppressWarnings

@Retention

@Retention is designed to be used only as an annotation to another annotation. It specifies the retention policy.

@Documented

The @Documented annotation is a marker interface that tells a tool that an annotation is to be documented.

@Target

The @Target annotation specifies the types of declarations to which an annotation can be applied.

It is designed to be used only as an annotation to another annotation. @Target takes one argument, which must be a constant from the ElementType enumeration.

Target Constant Annotation Can Be Applied To

ANNOTATION_TYPE
Another annotation
CONSTRUCTOR
Constructor
FIELD
Field
LOCAL_VARIABLE
Local variable
METHOD
Method
PACKAGE
Package
PARAMETER
Parameter
TYPE
Class, interface, or enumeration

You can specify one or more of these values in a @Target annotation. To specify multiple values, you must specify them within a braces-delimited list. For example:


@Target( { ElementType.FIELD, ElementType.LOCAL_VARIABLE } )

@Inherited

@Inherited is a marker annotation that can be used only on another annotation declaration. It affects only annotations that will be used on class declarations. @Inherited causes the annotation for a superclass to be inherited by a subclass.

@Override

@Override is a marker annotation that can be used only on methods. A method annotated with @Override must override a method from a superclass.

@Deprecated

@Deprecated is a marker annotation. It indicates that a declaration is obsolete and has been replaced by a newer form.

@SuppressWarnings

@SuppressWarnings specifies that one or more warnings that might be issued by the compiler are to be suppressed. The warnings to suppress are specified by name, in string form. This annotation can be applied to any type of declaration.

Home 
  Java Book 
    Language Basics  

Annotations:
  1. Annotations (Metadata)
  2. Retention Policy
  3. Annotation Default Values
  4. Marker Annotations
  5. Single-Member Annotations
  6. Built-In Annotations