What do we mean by Annotations in Java programming language ?.

What are Annotations in Java ?.

Annotations in Java are metadatas that adds information to your code in a formal way so that you can keep track to the information added at a later stage. They provide a Java programmer information which describes the program fully in a sophisticated way. Annotations help us in generating descriptor files and giving us way to write boilerplate code each time.

Annonations in Java have a symbol @ in its syntax declaration. Java contains general purpose built in annotations defined in java.lang package as :

1. @Override : This annotation is used over method which we intend to override in sub-class. It generates compile-time error if we accidentally give method signature incorrect at the time of overriding.

2. @Deprecated : Generally, it gives a programmer a compiler warning if he wishes to use a thing which is marked as Deprecated.

3. @SuppressWarnings : Generally, it is used when we want compiler to turn off the warnings it is displaying.

There are meta-annotations in Java programming language too that provides information while creating an annotation. Some of the meta-annotation are as follows :

1. @Target : This meta-annotation gives information about a annotation that where it is supposed to be applied. It has property as ElementType. The values it takes is of form :

i.   CONSTRUCTOR : For Constructor declarations.
ii.  FIELD : For Field declarations.
iii. LOCAL_VARIABLE : For local variable declarations.
iv.  TYPE : For class , interface , enum declarations.
v.   PARAMETER : For parameter declarations.
vi.  PACKAGE : For the package declarations.
vii. METHOD : For the method declarations.

2. @Retention : This meta-annotation gives general limit as how long the information in annotation should be kept. It has property as RetentionPolicy. The values it takes is of form :

i.   SOURCE : The annotation usage is at source level and generally discarded by the compiler.
ii.  CLASS : The annotation usage is at class file level, used by the compiler and generally discarded by the VM.
iii. RUNTIME : The annotation usage is at runtime level, used by the VM to be read reflectively.

3. @Documented : This meta-annotation is mostly used in the javadocs.

4. @Inherited : This meta-annotation allows all the sub-classes to inherit parent annotations.