Annotations in Java

Annotations are used to provide metadata about class or method in java and can be used while compiling the program. Annotations were introduced in Java 1.5 version. Before that, we had an XML file for configuration. We write annotations with the ‘@’ sign, for example, @Override annotation. Here ‘@’ sign tells the compiler about the following annotation. There are seven built-in popular used annotations in java which are divided into two parts.

There are four annotations called meta-annotations. These are in java.lang.annotations package and known as annotations about annotations.

  • Target
  • Retention
  • Inherited
  • Documented

The other three annotations are

  • Override 
  • SuppressWarnings
  • Deprecated

Target:

All the annotations in java have to have two properties- Target and Retention.

Target means where we want to use these annotations. For this target property, we need to use @Target annotation. 

This annotation is a single-valued annotation with 8 possible values.

ANNOTATION_TYPE, CONSTRUCTOR, FIELD, LOCAL VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE(Classes, Interface or ENUM).

Retention:

The meaning of retention in general terms continues to hold. Generally, we have 3 different stages in our coding

Source code(.java)  >>>  Compiler(.class)  >>>  Runtime

Based on our requirement we can decide to what point we want to keep this data or extra information in our code. @Retention annotation is also a single-valued annotation return enum.

It has 3 possible values. SOURCE, CLASS(DEFAULT), RUNTIME

Inherited:

We can use this annotation to make our custom annotation inheritable. By default, annotations are not inheritable. If we want to create an annotation the first step is to declare it with @interface. And if we want that the created annotation can also be inherited by another class, we have to add @inherited while creating it.

Documented:

This annotation indicates that the particular annotation is to be documented by JAVADOC or a similar tool. This is also a marker annotation. The retention policy of this annotation is runtime because of JAVADOC

Override Annotation:

It is a marker annotation which means it doesn’t have any element or value. This annotation is used when we override a method from a parent class. The use of this annotation is to make sure that the method is overridden or not. Suppose we are using overriding in our java application and we misspelled the name of the function. Then it will work as a new function instead of an overridden function. The @Override annotation will show an error to check the name.

In the above example, we can see that the name of the overridden function is different so it is showing an error that method taste is not present in supertype ‘vehicle’.

The other benefit of this annotation is that we can find the annotation by which interface or class we are overriding the method. If the method is successfully overridden, we can find the method by which it is overridden.

SuppressWarnings:

@SuppressWarnings annotation is used to suppress the generated warning by the compiler. The compiler will simply ignore the warning if it appears in a code written with @SuppressWarnings  annotation. This annotation can be used with Type, Field, Method, Constructor, and Local_variable. If we use this annotation at the class level then the compiler will ignore all warnings in the method. If we use it on the method level then all other warnings will be shown except for the particular method.

However, a warning is something potentially wrong with the code, so if you are getting any warning then the first option should be resolving the error.

But if you are suppressing any warning for a reason, you should comment on the reason so that other developers can also know why are you suppressing that particular warning. 

Possible values inside the Annotation element:

All, Cast, Deprecation, divzero, empty, unchecked, fallthrough, path, serial, finally and overrides.

If we are stuck because of some known warning then this will help to ignore the warning and move ahead. Commonly we suppress deprecation and unchecked warnings.

Deprecated:

This annotation is used to generate a warning if anyone uses a particular function. The compiler will generate a warning if a deprecated component is used or overridden in non-deprecated code. It informs the user that it may be removed in future versions. It can be used in any class, interface, method, or constructor.

Deprecated annotation improves the readability of the code and it is also helpful for code maintenance.

Leave a Reply