Annotations in Java

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.

Some points about Annotations are:

  • Annotations start with ‘@’ and can contain attribute/value pairs called elements
  • They do not change the action or execution of a compiled program.
  • Annotations help to associate metadata or information to the elements of the program like classes, instance variables, interfaces, constructors, methods, etc.
  • We cannot consider Annotations as pure comments as they can change the way a compiler treats a program

Couple of rules about Annotations are:

  • Annotations function a lot like interfaces. 
  • Annotations establish relationships that make it easier to manage data about our application
  • Annotations ascribes custom information on the declaration where it is defined.
  • Annotations are optional metadata and by themselves do not do anything.

Hierarchy of Annotations in Java 

Custom Annotations

Java allows you to create your own metadata in form of custom annotations. You can create your own annotations for specific purposes and use them as well.

To create a custom annotation, you must use the keyword “@interface“. Other important things to remember while creating custom annotations are listed below:

  • Each method declaration defines an element of the annotation type.
  • Method declarations must not have any parameters or a throws clause.
  • Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types.
  • Methods can have default values.

Example 1: Without Any Elements

Example 2: Required Elements

Example 3 : Mixing Required and Optional Elements

Example 4 : Creating a value() Element

Example 5: Passing an Array of Values

Build-In Annotations

@Target

Use @Target annotation to restrict the usage of new annotation on certain java elements such as class, interface or methods. After specifying the targets, you will be able to use the new annotation on given elements only.

@Retention

This annotation specifies how the marked annotation is stored in java runtime. Whether it is limited to source code only, embedded into the generated class file, or it will be available at runtime through reflection as well.

@Documented

This annotation indicates that new annotation should be included into java documents generated by java document generator tools.

@Inherited

When this annotation is applied to a class, subclasses will inherit the annotation information found in parent class.

For Example: @MyCustomAnnotation annotation is applied to both TestAnnotation and TestCheckAnnotation class due to @Inherited annotation in custom annotation.

@Repeatable

By default, an annotation is applied on a java element only once. But, by any requirement, you have to apply a annotation more than once, then use @Repeatable annotation on your new annotation.

Common Annotations

@Override

This annotation checks that the annotated method is overridden method. It causes a compile time “error” if the annotated method is not found in one of the parent classes or implemented interfaces. Very useful annotation and I will recommend to use it frequently.

@Deprecated

Use this annotation on methods or classes which you need to mark as deprecated. Any class that will try to use this deprecated class or method, will get a compiler “warning“.

@SuppressWarnings

This annotation instructs the compiler to suppress the compile time warnings specified in the annotation parameters. e.g. to ignore the warnings of unused class attributes and methods use @SuppressWarnings("unused") either for a given attribute or at class level for all the unused attributes and unused methods.

@SafeVarargs

Marker annotations indicates that method does not perform any potential unsafe operations on its varargs parameters. It can be applied only constructors or methods that can not be overridden(private, static or final)

@FunctionalInterface

This annotation is used to mark an interface as functional interface which are introduced in java 8. To read more about functional interfaces please follow the linked post.

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*