Jump to content

Java annotation

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dmccreary (talk | contribs) at 13:40, 16 February 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

In programming Java annotations is a way of adding metadata to Java source code that is available to the programmer at run-time.

Java annotations can be added to program elements such as classes, methods, fields, parameters, local variables, and packages

History of Java annotation

Java annotations were introduced with the JDK version 1.5. The feature was added to the Java language through the JSR-175 specification.

How Java annotations are processed

The compiler can store the metadata in the class files. Later, the VM or other programs can look for the metadata to determine how to interact with the program elements or change their behavior.

Syntax of annotation

Declaring an annotation is a variation tags that have been added to comment sections in the past.

Annotations take the form of an interface declaration with an @ preceding it and optionally marked with meta-annotations, as shown below:

 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.METHOD})

In the above example both Retention and Target are examples of annotations.

Pros of using Java annotations

Annotations allow the programmer to declare in their source code how the software should behave. It is an example of how declairative programming constructs can be added to a procedural lanaguage.

Cons of using Java annotations

  • Adding metadata to a run time causes additional memory overhead.
  • There are few standards that dictate what metadata tags should be used

See also

References

{compu-sci-stub}