Java defines has built-in annotations.
We will take a look of the following built-in annotations.
@Retention
is used to annotate another annotation.
It specifies the retention policy.
@Documented
annotation tells that an annotation is to be documented.
It is used only as an annotation to an annotation declaration.
@Target annotation specifies the types of items that an annotation can be applied.
It is used as an annotation to another annotation.
The target constants are shown here:
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 |
TYPE_PARAMETER | Type parameter |
TYPE_USE | Type use |
We can specify one or more of these values in a @Target annotation.
To specify multiple values, specify them within a braces-delimited list.
@Target( { ElementType.FIELD, ElementType.LOCAL_VARIABLE } )
@Inherited is a marker annotation used only on another annotation declaration.
@Inherited causes the annotation for a superclass to be inherited by a subclass.
@Override is a marker annotation for on methods.
A method with @Override must override a method from a superclass.
@Deprecated is a marker annotation that indicates that a declaration is obsolete and has been replaced by a newer form.
@FunctionalInterface is a marker annotation for interfaces.
It marks that the annotated interface is a functional interface.
@SafeVarargs is a marker annotation for methods and constructors.
It indicates that no unsafe actions related to a varargs parameter occur.
@SuppressWarnings can suppress warnings by compiler.