The Java specification describes an annotation as a marker that associates information with the program construct but has no effect at runtime.
Oracle Annotations Specification
Java annotations are like Javadoc tags which can be read from source files, but unlike Javadoc tags, annotations can be configured
(using a Retention Policy) to be retained invisible to the Java virtual machine, allowing them to be accessible at runtime.
Annotations are often used by frameworks (such as Spring or the Java persistence API) as a way to apply behaviors to user defined classes and methods which would otherwise be declared in an external source (such as an XML configuration file) or by requiring the implementation of specified API calls.
Two Types of Annotations
There are two types of annotations that specify what is being annotated:
The declaration annotation or the
Type annotation. The declaration annotation allows you to annotate any or all of the following types in Java.
MODULE
PACKAGE
ANNOTATION_TYPE
TYPE
TYPE_PARAMETER
CONSTRUCTOR
METHOD
PARAMETER
FIELD
LOCAL_VARIABLE
As of the Java SE8 release, annotations can also be applied to the following usages of types:
instantiating an object,
Casting, or
implementing an interface.
Retention Policy
An annotation has something called a retention policy which specifies to the compiler, whether the annotation will be specified (retained) in the generated class file and whether ultimately it is information which gets picked up by the JVM which can then be read using reflection methods.
Retention Policy Code in .class file Retained by JVM -
Read Reflectively
SOURCE NO NO
CLASS YES NO
RUNTIME YES YES
An annotation is recognized by the @ sign. In its simplest form and annotation looks like:
@MyFirstAnnotation
An annotation can also include elements with values. The elements can be named or unnamed.
The named element in the example below is ânameâ.
@MyFirstAnnotation (name=âTHISâ)
An unnamed element is allowed if only one element is configured for the annotation and his named and value.
@MySecondAnnotation(âLASTâ)
can be used in place of :
@MySecondAnnotation(value=âLASTâ)
If the annotation has no elements, then the parentheses can be omitted:
@MyElementlessAnnotation
can be used in place of:
@MyElementlessAnnotation()
You can use multiple annotations to describe a single declaration.
@MySecondAnnotation("LAST")
@MyFirstAnnotation(name="FIRST")
public void method(){
A public method where 2 annotations appear above the method declaration. You can use the same annotation with different element values to describe a single declaration.
This feature is only available if the adaptation is declared repeatable.
Example: An example of using a repeatable annotation is shown below.
@MyFirstAnnotation(name=âTHISâ)
@MyFirstAnnotation(name=âTHATâ)
public class AnnotationExamples{
Three Categories of Annotations
There are three categories of annotations based upon the elements declared in the annotation.
The Java specification names these as
Normal,
Marker, and
Single element.
Note that both Marker and Single elements annotations are also "Normal" annotations with special identifying features
(No element specified or a single element (value) specified but not named). A normal annotation must contain an element value pair for every element of the corresponding annotation type, except for those elements with default values, or a compile time error occurs.
Types of Annotation Element Values
Marker 1) Specifies no element values , 2) Specifies element/value mappings but all have a default value
Examples
@MyElementlessAnnotation
Single Element
Specifies only a single element value named value
Specifies element/value mappings in addition to the element named value, but all other elements are declared with a default value
Note that value is the unnamed element in the use of the Annotation
@MySecondAnnotation("THIS")
Normal:
Specifies zero to many element values
Specifies zero to many element default values
@MyFirstAnnotation(name="THAT", value="MORE")
Annotations Overview
Creating a custom annotation is easy and edit simplest looks like the following
public @interface MyInterface{
}
When you create a new class in IntelliJ, you can specify an annotation type.
To specify properties about an annotation you annotates the annotation type with one or all of the following annotations, found in