blog

Home / DeveloperSection / Blogs / Annotations in Java: Why we use Annotations?

Annotations in Java: Why we use Annotations?

Jonas Stuart2302 28-May-2016

One of the major goals of any IDEsuch as NetBeansor Eclipseis to enhance ease of development. The IDE generally provides the boilerplate code for many types of applications so that the developer can focus on the core functionality of the application. However, having source code that does not contain any boilerplate code makes it easier to maintain and create a bug-free application.

  •  Annotations help achieve this goal by facilitating the tool vendors in generating the boilerplate code.
  •  Besides this, annotations help in code analysis and checking.
  •  Other uses of annotations include documenting our code for the benefit of fellow developers and providing vital runtime information to testing tools.

We’ll learn all these benefits as we read this section. Annotations are like meta-tags—a data about data. One such annotation (meta-tag) we have already come across in Java’s documentation is @Deprecated.

J2SE 5.0 added many more annotations, as well as added the facility for creating our own annotations. Annotations (also known as metadata) provide a formalized way to add information to our code so that we can easily use that data at some later point.

Annotations are partly motivated by a general trend toward combining metadata with source-code files, instead of keeping it in external documents. They are also a response to feature pressure from other languages like C#.

An annotation is a mechanism in the Java language that allows developers to attach information to different parts of their code. Two instances of annotations we might have noticed while surfing through Java’s documentation are

  •     @Override
  •    @Deprecated

Annotations do not become a part of our code in the sense that they do not alter the code behaviour at runtime. They also do not change the code semantics.

They are helpful in indicating:
  •    Whether our methods are dependent on other methods,
  •    Whether our methods are incomplete
  •   Whether our classes have references to other classes, and so on.

They simply help tool vendors to assist us in writing error-free code. Deployment tools such as the EJB (Enterprise JavaBeans) deployer can also use annotations to achieve error-free deployment.

We will discuss the annotations relevant to the Java language and not consider the ones used by deployment tools.

Annotations can replace existing systems like XDoclet, which is an independent doclet tool  that is specifically designed for creating annotation-style doclets. In contrast, annotations are true language constructs and hence are structured, and are type-checked at compile time. Keeping all the information in the actual source code and not in comments makes the code neater and easier to maintain. By using and extending the annotation API and tools, or with external bytecode manipulation libraries as we will see, we can perform powerful inspection and manipulation of our source code as well as the bytecode.

Built-in Annotations:
We will cover the following three built-in annotations:

·     @Deprecated

·     @Override

·     @SuppressWarnings

We will learn the use of @Deprecated annotation in the next posts.

Updated 15-Mar-2018

Leave Comment

Comments

Liked By