Java Reference
In-Depth Information
Compare the declaration of the Version annotation with the declaration of an interface. It differs from an
interface definition only in one aspect: it uses the @ sign before its name. You have declared two abstract methods in
the Version annotation type: major() and minor() . Abstract methods in an annotation type are known as its elements.
You can think about it in another way: an annotation can declare zero or more elements, and they are declared
as abstract methods. The abstract method names are the names of the elements of the annotation type. You have
declared two elements, major and minor , for the Version annotation type. The data types of both elements are int .
although it is allowed to declare static and default methods in interface types, they are not allowed in
annotation types.
Note
You need to compile the annotation type. When Version.java file is compiled, it will produce a Version.class file.
The simple name of your annotation type is Version and its fully qualified name is com.jdojo.annotation.Version .
Using the simple name of an annotation type follows the rules of any other types (e.g. classes, interfaces, etc.).
You will need to import an annotation type the same way you import any other types.
How do you use an annotation type? You might be thinking that you will declare a new class that will implement
the Version annotation type, and you will create an object of that class. You might be relieved to know that you do not
need to take any additional steps to use the Version annotation type. An annotation type is ready to be used as soon
as it is declared and compiled. To create an instance of an annotation type and use it to annotate a program element,
you need to use the following syntax:
@annotationType(name1=value1, name2=value2, names3=values3...)
The annotation type is preceded by an @ sign. It is followed by a list of comma-separated name=value pairs
enclosed in parentheses. The name in a name=value pair is the name of the element declared in the annotation type
and the value is the user supplied value for that element. The name=value pairs do not have to appear in the same
order as they are declared in the annotation type, although by convention name=value pairs are used in the same
order as the declaration of the elements in the annotation type.
Let's use an annotation of the Version type, which has the major element value as 1 and the minor element value
as 0. The following is an instance of your Version annotation type:
@Version(major=1, minor=0)
You can rewrite the above annotation as @Version(minor=0, major=1) without changing its meaning. You can
also use the annotation type's fully qualified name as
@com.jdojo.annotation.Version(major=0, minor=1)
You use as many instances of the Version annotation type in your program as you want. For example, you have
a VersionTest class, which was added to your application since release 1.0. You have added some methods and
instance variables in release 1.1. You can use your Version annotation to document additions to the VersionTest
class in different releases. You can annotate your class declaration as
@Version(major=1, minor=0)
public class VersionTest {
// Code goes here
}
 
Search WWH ::




Custom Search