Java Reference
In-Depth Information
15.1. A Simple Annotation Example
Consider an informal, comment-based class preamble:
/*-------------------------------
Created: Jan 31 2005
Created By: James Gosling
Last Modified: Feb 9 2005
Last Modified By: Ken Arnold
Revision: 3
---------------------------------*/
public class Foo {
// ...
}
You can define an annotation type that can hold all of the desired inform-
ation:
@interface ClassInfo {
String created();
String createdBy();
String lastModified();
String lastModifiedBy();
int revision();
}
An annotation type is a special kind of interface, designated by the
@
program elements,
such as a class or field declaration. What appear to be
methods in the annotation type are actually special
annotation element
declarations. An annotation element is rather like a field, and has a value
for each program element that has that type of annotation. So a
ClassInfo
annotation contains four string elements and one
int
element. When you
annotate
(apply an annotation to) a program element, you supply val-