HomeJavaUnderstanding Java Annotation with Example

Understanding Java Annotation with Example

Annotations were introduced to the Java world with the version 1.5 and the main purpose of Annotations is to provide information about the code without having any direct effect on the code which they Annotate.

Java Annotation is the metadata about the program embedded in the program itself. We can specify Annotation availability to either compile time or runtime.

Java program metadata was available through Java comments or by Javadoc before the introduction of the Annotation.

[quads id=1 /]

Custom Annotation

Creating custom annotation in Java is similar to writing an Interface. The @interface element is used to declare an annotation. Letā€™s see an example of Java custom annotation.

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodInfo{
	String author() default "Developers Journal";
	String date();
    String comments();
}

[quads id=2 /]

Letā€™s understand the annotations used in the above example:

@Documented ā€“ This annotation indicates that elements using this annotation should be documented by Javadoc and similar tools and hence become part of the public API.

@Target ā€“ This annotation indicates the kinds of program element to which an annotation type is applicable. Possible values that are used with this annotation are METHOD, TYPE, CONSTRUCTOR, FIELD, etc.

@Inherited ā€“ This annotation indicates that an annotation type is automatically inherited. Ā If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class’s superclass will automatically be queried for the annotation type. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached. If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation.

@Retention ā€“ This annotation indicates for how long annotations with the annotated type are to be retained. If no Retention annotation is present on an annotation type declaration, the retention policy defaults to RetentionPolicy.CLASS. Possible values that are used for RetentionPolicy are CLASS, RUNTIME, SOURCE.

Some of the important points to take care while declaring custom annotations are:ā€¢ Annotation methods cannot have parameters.ā€¢ Return types are limited to primitives, Strings, Enums, Annotations or array of these.ā€¢ Annotation methods can have default values.

[quads id=3 /]

Java Annotation

Letā€™s understand Java Annotation with the help below example:

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

public class AnnotationExample {

	public static void main(String[] args) {
	}

	@Override
	@MethodInfo(author = "Developers Journal", comments = "Main method", date = "June 22 2017")
	public String toString() {
		return "Overriden toString method";
	}

	@Deprecated
	@MethodInfo(comments = "This is a Deprecated method", date = "June 22 2017")
	public static void oldMethod() {
		System.out.println("This is deprecated, don't use it anymore");
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	@MethodInfo(author = "Developers Journal", comments = "Main method", date = "June 22 2017")
	public static void genericsTest() throws FileNotFoundException {
		List l = new ArrayList();
		l.add("abc");
		oldMethod();
	}
}

There is three Java built-in annotation:

@Override ā€“ This annotation should be used to inform the compiler that we are overriding the method of Superclass.

@Deprecated ā€“ This annotation should be used when we want the compiler to know that the particular method is deprecated. We should provide information for why this method is deprecated and what is the alternative to use as per the recommendation of Java.

@SuppressWarnings ā€“ This annotation should be used to tell the compiler to ignore specific warnings that the particular method produces.

RELATED ARTICLES

Most Popular