Annotations in Java

Java annotations are essentially tags used to provide Meta data for your Java code. These are extra details that can be utilised by the java compiler or JVM. Being Meta data, the annotations do not directly affect the execution of your code, although some types of annotations can actually be used for that purpose. Java annotations were added to Java from Java 5.


Java annotations are typically used for the following purposes:

  • Compiler instructions
  • Build-time instructions
  • Runtime instructions

Java has three built-in annotations that you can use to give instructions to the Java compiler. These annotations are explained in more detail later in this chapter.

Java annotations can be used at build-time, when you build your software project. The building process includes generating source code, compiling the source, generating XML files (e.g. deployment descriptors), packaging the compiled code and files into a JAR file, etc.

Building the software is typically done by an automatic build tool like Apache Ant or Apache Maven. Build tools may scan your Java code for specific annotations and generate source code or other files based on these annotations.

Normally, Java annotations are not present in your Java code after compilation. It is possible, however, to define your own annotations that are available at runtime.

These annotations can then be accessed via Java Reflection, and used to give instructions to your program, or some third party API.

An annotation in its shortest form looks like this:
@SetValueJbk

The @ character signals to the compiler that this is an annotation. The name that follows the @character is the name of the annotation.

 @SetValueJbk(value = "Kiran JBK")  String name;

The annotation in this example contains a single element named name, with the value set to Kiran JBK. Here, the elements are enclosed inside the parentheses after the annotation name. Annotations without elements do not need parentheses.

Annotation Placement:

You can put Java annotations above classes, interfaces, methods, method parameters, fields and local variables.

1) SetValueJbk.java


2) Student.java


3) AnnotTest.java


If there is NO annotation, then we need to set and get values and dynamic usage is not possible. The same thing is internally implemented in Hibernate, which we do by using annotations.

Java comes with three annotations which are used to give the Java compiler instructions.
These annotations are:

  • @Deprecated
  • @Override
  • @SuppressWarnings

Each of these annotations is explained in the following sections:

Deprecated:

The @Deprecated annotation is used to mark a class, method or field as deprecated, meaning it should no longer be used. If your code uses deprecated classes, methods or fields, the compiler will give you a warning.
Here is an example:

     @Deprecated
     public class MyComponent {
     }

The use of the @Deprecated annotation above the class declaration marks the class as deprecated.

You can also use the @Deprecated annotation like the above method and field declarations to mark the method or field as deprecated. When you use the @Deprecated annotation, it is a good idea to also use the corresponding @deprecated JavaDoc symbol, and explain why the class, method or field is deprecated, and what the programmer should use instead.


Override:

The @Override annotation is used above methods that override methods in a superclass. If the method does not match a method in the superclass, the compiler will give you an error. It is one of the reasons why it is considered to be a good practice while coding in java.

The @Override annotation is not necessary in order to override a method in a superclass. It is a good idea to still use it though. In case someone changed the name of the overridden method in the superclass, your subclass method would no longer override it.

Without the @Override annotation you would not find out what happened.

With the @Override annotation, the compiler will tell you that the method in the subclass is not overriding any method in the superclass.

Here is an example use of the @Override annotation:

In case the method m1() in A changes signature so that the same method in the subclass no longer overrides it, the compiler will generate an error.


Suppress Warnings:

The @Suppress Warnings annotation makes the compiler suppress warnings for a given method or the annotated element. For instance, if a method calls a deprecated method, or makes an sinsecure type cast, the compiler may generate a warning.

You can suppress these warnings by annotating the method containing the code with the @SuppressWarnings annotation. For example:

     @SuppressWarnings
     public void methodWithWarning() {
     }