Technology: Annotations

Overview

Annotations provide a standard way to add metadata (data about data) to Java classes, methods and variables. Annotations (starting with @) are like Javadoc except that they are not intended for humans but for programs or tools which can read them from source or byte code to provide special functionality.

For example, annotations could provide information for a code-processor what methods of a class should be published as Webservice (e.g., CXF provides this functionality). To make the system running, a pre-processing step (before compilation) is needed. This preprocessing could be initiated within the Maven build and (in the given example) would read the annotation, create the webservice description files and so on.

Usage

In Java 1.5, a set of annotations where introduced to allow the compiler to detect errors or suppress warnings.

E.g. if you override a method of a super class, it can occur that you make a spelling error and write as method name initialise instead of initialize. So you never override the method and your program may deliver false output. In the worst case, you never realize this. The annotation @Override is used by the compiler to check if a method is overriden:
@Override
void myOverridenMethod() { }
            

Reference

A Guide to annotations can be found on the Oracle documentation site for Java 1.5.0. People interested in the original Java Specification Request can go to the JSR-175.