How annotation works in Java?

In Java, annotations are metadata about the source code. They do not have any direct effect on the execution of the Java program. Annotations in Java were introduced in JDK 5. The main purpose of using annotation is that it gives instructions to the compiler at the build-time and at the runtime of program execution.

Built-in Annotation in Java

There are three built-in annotations in Java,

1. @Override

The annotation is used while we perform inheritance. The child class overrides the method declared in the base class. It is recommended to use this annotation so that even if someone changes the name of the method in the base class, the compiler will show an error message. Otherwise, it can be a hectic task to identify such errors.

Example:

Here, the dummymethod() is initially declared inside SampleParent class and overridden inside SampleChild class.

2. @Deprecated

The @Deprecated annotation is a type of built-in annotation used for the data members who are no longer used in the program. The compiler shows a warning message if a @Deprecated data member is called or used. It is recommended to us @Deprecated annotation to explain the reason behind deprecating the method, class, or data variable.

Example:

Here, the class SampleAnnotate is deprecated using the @Deprecate annotation. And @deprecated symbol explains the reason.

3. @SuppressWarnings

Here, the deprecatedmethod() is deprecated in the base class but still can be overridden using @SuppressWarnings.

Types of Annotations

1. Marker Annotation

The purpose of marker annotation is to label a declaration. It does not contain any kind of data or members.

Example: @Override

2. Single value Annotation

An annotation that only contains one data value or method is called Single value annotation. The data element is named as value by default.

Example: @Annotaionexample("DataMemberName")

3. Full Annotation

This kind of annotation has multiple data elements split by using a comma.

Example: @Annotaionexample(value1=" DataMemberName", value2=" DataMemberName")

4. Type Annotation

Type annotation can be used to annotate the type of any method or a data variable. Type annotation is annotated using @Target annotation.

Example: The following program demonstrates the type annotation.

SampleTypeAnnotation.java

Output:

This program demonstrates type annotation
This method return type uses type annotation

How to Create Custom Annotation?

In order to create a custom annotation, the @interface annotation is used. It informs the Java compiler about the custom annotation. The following example shows how to create a custom annotation.

The following code shows the use of custom annotation declared above.

All the data members declared inside the custom annotation are initialized in the above code.

Annotation Placement

1. Levels of Annotation

The annotations in Java program can be placed at the start of a class, method, variable, or interface. Following are the two levels of annotations.

i) Class Level Annotation:

The annotation that is placed just above the class is called a Class level annotation.

Example:

Here, @Entity is a custom class-level annotation.

ii) Method Level Annotation:

The annotation that is placed just above a method is called a Method level annotation.

Example:

In the above code, the @SampleAnnotation is a custom annotation.

2. Type Annotation

At first, programmers were allowed to only use annotations at the declaration part of data members. But now we can use annotation wherever a type is declared.

Example:

The above example declares a String type of List with @NonNull annotation.






Latest Courses