Wednesday 27 July 2016

Java Annotations

Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.

Uses of Annotations:
Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
Runtime processing — some annotations are available to be examined at runtime.

Example of Java annotations:
To describe constraints or usage of an element: e.g. @Deprecated, @Override, or @NotNull.
To describe the "nature" of an element: e.g. @Entity, @TestCase, @WebService.
To describe the behavior of an element: @Statefull, @Transaction.
To describe how to process the element:  @Column, @XmlElement.

In all the above cases, annotations are used to describe the element and clarify its meaning.

Note:
Prior to JDK5, information that is now expressed with annotations needed to be stored somewhere else, and XML files were frequently used. However it is more convenient to use annotations because they will belong to the Java code itself, and are hence much easier to manipulate than XML.

Advantages of the annotation:
1) All the information is in a single file (no need to open two files to configure a given behavior).
2) When the class changes, no need to modify the xml file.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...