Java Annotation is a tag that represents the metadata i.e. attached with class, interface, methods or fields to indicate some additional information which can be used by java compiler and JVM.
Annotations in Java are used to provide additional information, so it is an alternative option for XML and Java marker interfaces.
First, we will learn some built-in annotations then we will move on creating and using custom annotations.
There are several built-in annotations in Java. Some annotations are applied to Java code and some to other annotations.
Let's understand the built-in annotations first.
@Override annotation assures that the subclass method is overriding the parent class method. If it is not so, compile time error occurs.
Sometimes, we does the silly mistake such as spelling mistakes etc. So, it is better to mark @Override annotation that provides assurity that method is overridden.
Output:
Comple Time Error
@SuppressWarnings annotation: is used to suppress warnings issued by the compiler.
Output:
sonoo vimal ratan
If you remove the @SuppressWarnings("unchecked") annotation, it will show warning at compile time because we are using non-generic collection.
@Deprecated annoation marks that this method is deprecated so compiler prints warning. It informs user that it may be removed in the future versions. So, it is better not to use such methods.
At Compile Time:
Note: Test.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.
At Runtime:
hello n
Java Custom annotations or Java User-defined annotations are easy to create and use. The @interface element is used to declare an annotation. For example:
Here, MyAnnotation is the custom annotation name.
There are few points that should be remembered by the programmer.
There are three types of annotations.

An annotation that has no method, is called marker annotation. For example:
The @Override and @Deprecated are marker annotations.
An annotation that has one method, is called single-value annotation. For example:
We can provide the default value also. For example:
Let's see the code to apply the single value annotation.
The value can be anything.
An annotation that has more than one method, is called Multi-Value annotation. For example:
We can provide the default value also. For example:
Let's see the code to apply the multi-value annotation.
@Target tag is used to specify at which type, the annotation is used.
The java.lang.annotation. ElementType enum declares many constants to specify the type of element where annotation is to be applied such as TYPE, METHOD, FIELD etc. Let's see the constants of ElementType enum:
| Element Types | Where the annotation can be applied |
|---|---|
| TYPE | class, interface or enumeration |
| FIELD | fields |
| METHOD | methods |
| CONSTRUCTOR | constructors |
| LOCAL_VARIABLE | local variables |
| ANNOTATION_TYPE | annotation type |
| PARAMETER | parameter |
@Retention annotation is used to specify to what level annotation will be available.
| RetentionPolicy | Availability |
|---|---|
| RetentionPolicy.SOURCE | refers to the source code, discarded during compilation. It will not be available in the compiled class. |
| RetentionPolicy.CLASS | refers to the .class file, available to java compiler but not to JVM . It is included in the class file. |
| RetentionPolicy.RUNTIME | refers to the runtime, available to java compiler and JVM . |
Let's see the simple example of creating, applying and accessing annotation.
Output:
value is: 10
In real scenario, java programmer only need to apply annotation. He/She doesn't need to create and access annotation. Creating and Accessing annotation is performed by the implementation provider. On behalf of the annotation, java compiler or JVM performs some additional operations.
By default, annotations are not inherited to subclasses. The @Inherited annotation marks the annotation to be inherited to subclasses.
The @Documented Marks the annotation for inclusion in the documentation.
We request you to subscribe our newsletter for upcoming updates.