Java Built In Annotations

Java defines many built-in annotations. Most of these are specialized, but nine are general purpose. Of these, four are imported from java.lang.annotation: @Retention, @Documented, @Target, and Inherited. Five, @Override, @Deprecated, @FunctionInterface, @SafeVarargs, and @SuppressWarnings, are included in java.lang.

Let's discuss these built-in annotations one by one.

@Retention

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

@Documented

The @Documented annotation is a marker interface that tells a tool, an annotation is to be documented. It is designed to be used only as an annotation to an annotation declaration.

@Target

The @Target annotation specifies the types of items to which an annotation can be applied. It is designed to be used only as an annotation to another annotation.

@Target takes only one argument, which is an array of constants of the ElementType enumeration. This argument determines the types of declarations to which the annotation can be applied. The constants are shown here (in this table) along with the type of declaration to which they corresponds :

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 (added by JDK 8)
TYPE_USE Type use (added by JDK 8)

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 instance, to specify that an annotation applies only to the fields and the local variables, you can use this @Target annotation:

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

If you don't use @Target, then except for type parameters, the annotation can be used on any declaration. For this cause, it is often a good idea to explicitly specify the target/targets so as to clearly indicate the intended uses of an annotation.

@Inherited

The @Inherited is a marker annotation that can be used only on another annotation declaration. Furthermore, it affects only annotations which will be used on class declarations.

@Inherited causes the annotation for a superclass to be inherited by a subclass. So, when a request for a specific annotation is made to the subclass, if that annotation isn't present in the subclass, subsequently its superclass is checked. If that annotation is present in the superclass, and if it is annotated with @Inherited, then that annotation will be returned.

@Override

The @Override is a marker annotation that can be used only on methods.

A method annotated with @Override must override a method from a superclass. If it does not, a compile-time error will produce. It is used to ensure that a superclass method is actually overridden, and not simply overloaded.

@Deprecated

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

@FunctionalInterface

The @FunctionInterface is a marker annotation added by the JDK 8 and designed for use on interfaces. It indicates that an annotated interface is a functional interface.

A functional interface is an interface that contains only one abstract method. Functional interfaces are used by the lambda expressions. If the annotation interface is not a functional interface, a compilation error will be produced.

It is important to understand that @FunctionInterface is not required to create a functional interface. Any interface with only one abstract method is, by definition, a functional interface. Thus, @FunctionInterface is purely informational.

@SafeVarargs

The @SafeVarargs is a marker annotation that can be applied to methods and constructors. It indicates that no unsafe actions linked to a varargs parameter occur. It is used to suppress unchecked warnings on otherwise safe code as it refers to non-reifiable varargs types and parameterized array instantiation. A non-reifiable type is, essentially a generic type. It must be applied only to varargs methods or constructors that are static or final.

@SuppressWarnings

The @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.

Java Online Test


« Previous Tutorial Next Tutorial »