Annotations in Java
Annotations in Java
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,
- @Override
It is used during 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:
/* Base Class */ public class SampleParent { public void dummymethod() { System.out.println("Inside parent class"); } } /* Child Class */ public class SampleChild extends SampleParent { @Override public void dummymethod() { System.out.println("Inside child class"); } }
Here, the dummymethod() is initially declared inside SampleParent class and overridden inside SampleChild class.
- @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 symbol to explain the reason behind deprecating the method, class, or data variable.
Example:
@Deprecated /* @deprecated Use another class.*/ public class SampleAnnotate { /* Some code */ }
Here, the class SampleAnnotate is deprecated using @Deprecate. And @deprecated symbol explains the reason.
- @SuppressWarnings
public class SampleAnnotate { @Deprecated /* @deprecated Use another method.*/ void deprecatedmethod() { /* Some code */ } } class Sample extends SampleAnnotate { @SuppressWarnings /* Deprecated method can be overridden with @SuppressWarnings */ void deprecatedmethod() { /* Some code */ } }
Here, the deprecatedmethod() is deprecated in the base class but still can be overridden using @SuppressWarnings.
Types of Annotations
- Marker Annotation
The purpose of marker annotation is to label a declaration. It does not contain any kind of data or members.
Example: @Override
- 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”)
- Full Annotation
This kind of annotation has multiple data elements split by using a comma.
Example: @Annotaionexample(value1=”DataMemberName”, value2=”DataMemberName”)
- 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
import java.lang.annotation.ElementType; import java.lang.annotation.Target; /* @target annotation */ @Target(ElementType.TYPE_USE) /* Declare type annotation */ @interface AnnotationTypeExample{} public class SampleTypeAnnotaion { /* Driver Code */ public static void main(String ar[]) { /* Annotating the type of a string */ @AnnotationTypeExample String string = "This program demonstrates type annotation"; System.out.println(string); Annotatemethod(); } /* Annotating return type of a method */ static @AnnotationTypeExample int Annotatemethod() { System.out.println("This method return type uses type annotation"); return 0; } }
Output:
This program demonstrates type annotation This method return type uses type annotation
How to Create Custom Annotations?
To create a custom annotation @interface keyword is used. This keyword informs the Java compiler about the custom annotation. The following example shows how to create a custom annotation.
/* Custom annotation */ @interface SampleCustomAnnotation { String empid(); String empname() String emppost() int empage(); } The following code shows the use of custom annotation declared above. /* Custom Annotation is used here */ @MyAnnotation( empid="42", empname="abc" emppost="manager", empage=28, ) public class MyClass { /* Some Code */ }
All the data members declared inside the custom annotation are initialized in the above code.
Annotation Placement
- 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.
- Class Level Annotation:
The annotation that is placed just above the class is called a Class level annotation.
Example:
@Entity public class Demo { /* Some Code */ }
Here, @Entity is a custom class-level annotation.
- Method Level Annotation:
The annotations that are placed just above a method is called a method level annotation.
Example:
@SampleAnnotation int Sample() { /* method code */ }
In the above code, the @SampleAnnotation is a custom annotation.
- Type Annotation
At first, programmerswere allowed to only use annotations at the declaration part of data members. But now we can use annotation wherever a type is declared.
Example:
@NonNull List<String> dummylist
The above example declares a String type of List with @NonNull annotation.
In this article, we have discussed the concept of annotations in Java, what their types are and how we can use custom annotations.