Java 5.0 introduced annotations among many things. Annotations are a way of adding metadata to Java elements such as classes, methods, variables, parameters and packages. An annotation in its simplest form looks something like this:


The ‘@’ character means that what follows is an annotation. One of the uses of annotations is to generate boiler code. However, they can be used in several interesting ways. Java defines certain built-in annotations such as @Override and @Deprecated. @Override is used to annotate methods that are overridden. When annotated the compiler makes sure that the method is actually overriding a method, else compilation will fail. For example, the following code annotates a method myMethod() with @Override. When this code compiles, the compiler ensures that myMethod() is actually overridden.

public void myMethod(){

You might be wondering what do we gain by @Override? Assuming that we do not have annotations, if while overriding myMethod(), I misspell it to be yourMethod(), the compiler will not generate any error. It will consider it to be just another method and detecting such a bug can be a bloody business. So, you can see how useful can a simple annotation be. But, that is not all. As we mentioned earlier, annotations can be used for much more amazing stuff. One of my favourites is Deuce STMSoftware Transactional Memory is one of the several ways of developing concurrent applications in a clean fashion. Deuce STM  provides support for STM via a “java agent” and @Atomic annotation.

Annotations are a very interesting feature and there is a lot to annotations which you can know by reading the Oracle documentation to read more. The purpose of this post is not to rewrite what is already there. Rather I would like to show you in the next post, an interesting way to use annotations to implement a plugin-like structure to your application.