Java’s Metadata And Annotations, Part 1 : Introduction

Abhimanyu
2 min readFeb 20, 2019

Java Annotations provide a way to specify metadata about various java constructs like Classes, Methods, constructors or package.

In this series of articles we will understand what is annotation, its purpose, need and runtime/compile time usage.

What is an annotation?

  • Annotation is an special kind of java construct that is used to attach metadata or additional information to java program elements
  • These elements could be a class, method, instance variable etc.
  • An annotation starts with the symbol @ followed by annotation type name and placed just before a program element being annotated.
  • Below is an example of the simplest annotation

@Override annotation depicts that this method is overriding a parent method.

  • You can annotate classes, methods, fields, parameters, variables, constructors or whole package (through package-info.java file).

Built-in Annotations

Java provides three standard user level annotations.

  1. @Override
  2. @Deprecated
  3. @SuppressWarnings

@Override

It lets us add a new additional compiler check to our code. Its presence on a method indicates that the method is intended to override a method in a super class. So if the compiler detects that the method is not overriding anything or improperly overriding then It throws error at compile time.

@Deprecated

Its presence on a method indicates that the method is either is not efficient or badly written suggesting not to use further. @Deprecated is generally accompanied by @deprecated which is a standard java documentation annotation and appears only in a java comment and can be used to provide additional description string about the deprecated method. Please note the difference between both annotation. One start with ‘@D’ appears outside a java comment and second starts with ‘@d’ and appears in a comment.

@SuppressWarnings

This annotation acts as a directive to the compiler telling it to ignore certain warnings within the annotated code element. For Example

We can suppress selected warnings only by passing the name of the warning type into the annotation.

--

--