Java Annotations

Introduction to Java Annotations

Java Annotation is a Java construct used to decorate classes, methods, fields, parameters, variables, constructors, etc. It contains some additional information that can be accessed by Java compilers and JVMs. Consequently, annotations in Java represent metadata (data about data), they are tags that can be added to classes, interfaces, methods, or fields to provide the Java compiler with additional information. Their purpose is to provide data about a program that is not part of it.

Adding annotations to code does not affect its operation directly. But unlike comments, they can influence the way a compiler treats a program. Java annotations can be used to replace XML and Java marker interfaces. In other words, it is a suitable alternative to both XML and Java tagging interfaces.

Declaration and Format of Annotations

In Java, annotations are declared using ‘@’. The syntax of annotations in Java is: ‘@AnnotationName’. Annotations may also include elements, i.e. members, attributes, or parameters. We can add any number of elements, from zero, one, or multiple elements. The value of these elements needs to be set.

An annotation can be placed above any declaration. Generally, Java annotations appear above class declarations, method declarations, interface declarations, and field declarations. As of Java 8, type annotations can be used i.e. annotations can be placed before a type.

Example 1: Suppose a method needs to be overridden, say method1 of superclass Parent in the subclass named Child. Here annotation @Override is used to indicate to the compiler that method ‘method1’ is overridden. This prevents any error in overriding the method, as the compiler will show a warning otherwise.

Example 2: In this example, @Deprecated is used to deprecate the addition method. But the addition method is used in the main method and the compiler shows a warning regarding this. To overcome such an error, the main method is annotated with @SuppressWarnings with a value as “deprecation” to suppress all deprecation warnings that arise in the main method.

Uses of Annotations in Java

There are many ways to use annotations:

  • To give instructions to the compiler, detect errors, and suppress warnings.
  • Software build tools use these annotations to generate code, XML files, and many other things at compile time.
  • During runtime, some annotations can be defined to give instructions to the program. Java Reflection is used to access these annotations. Reflection provides a way to get the annotation (of a class, method, or field) and perform logical operations based on the annotation status.

Some of the commonly used Annotations in Java

There are a few most commonly used annotations in java. They are:

  • @Override: used to inform the compiler the method is meant to override a method declared in the parent class. It is not necessary to use this annotation while overriding but it prevents error.
  • @Deprecated: used to indicate that the marked method, class, or field is deprecated and should no longer be used.
  • @SuppressWarnings: used to suppress or ignore warnings coming from the compiler.
  • @FunctionalInterface: used to indicate that an interface type declaration is intended to be a functional interface.
  • @SafeVarargs: used to take var args parameters on a method or constructor. It can only be applied to final methods, static methods, and constructors.
  • @Target: used to indicate the context in which an annotation is applicable.
  • @Retention: it is a meta-annotation used to indicate for how long annotations with the annotated type are to be retained.
  • @Inherited: used to mark an annotation to be inherited to subclasses of the annotated class.
  • @Documented: used to annotate our custom annotations.
  • @Value: used at the field or method/constructor parameter level to indicate a default value.

Classification and Categorization

Annotations are broadly classified into three types namely predefined annotations, Meta annotations, and custom annotations. Predefined or built-in annotations are general purpose annotations found inside the Java.lang package (for example @Override, @SuppressWarnings, etc.), Meta annotations come under the java.lang.annotation package (for example @Target, @Inherited, etc.) and custom annotations are user-defined annotations.

Otherwise, Java annotations can also be categorized into – Marker Annotations, Single value Annotations, Full Annotations, Type Annotations, and Repeating Annotations.

Special thanks to Shivam Raj Sharma for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this articleIf you want to suggest any improvement/correction in this article please mail us at [email protected]