Annotations

From Wikibooks, open books for an open world
Jump to: navigation, search

Java Programming/Annotations
Introduction
Custom Annotations
Navigate Javadoc & Annotations topic: v  d  e )


Introduction[edit]

In Java, an annotation is a language construct that was introduced in J2SE 1.5 that provides a mechanism for including metadata directly in the source code.

Annotations can provide metadata for java classes, attributes, and methods. Syntactically, annotations can be viewed as special kind of modifier and can be used anywhere that other modifiers (such as public, static, or final) can be used

One of the main forces of adding this feature to Java was the wide spread use of XML descriptors to add additional information, metadata for Java classes. Frameworks like EJB, JSF, Spring, Hibernate were heavily using external XML descriptors. The problem of those external descriptors is that those files are out of reach of the Java compiler and for that reason compiler type checking could not be used. A small spelling mistake bug in a huge XML descriptor file is hard to locate and fixed. The Java annotations on the other hand use the Java compiler type checking features, so annotation names spelling mistakes will be caught by the Java compiler.

In summary, annotations can be...

  • used as a source of information for the compiler;
  • made available for compile-time or deployment-time processing;
  • examined at runtime.

External links[edit]

  • [1] The Java™ Tutorial on Annotations


Clipboard

To do:
Add some exercises like the ones in Variables.

Java Programming/Annotations
Introduction
Custom Annotations