to any other element types. The argument to
@Target
Annotation can be one fromthe possible set of values of any Java Element, which is defined in a well-definedEnum called
ElementType
. Here are the possible values taken by this Enum,
•
TYPE – Applied only to Type. A Type can be a Java class or interface or anEnum or even an Annotation.
•
FIELD – Applied only to Java Fields (Objects, Instance or Static, declared atclass level).
•
METHOD – Applied only to methods.
•
PARAMETER – Applied only to method parameters in a method definition.
•
CONSTRUCTOR – Can be applicable only to a constructor of a class.
•
LOCAL_VARIABLE – Can be applicable only to Local variables. (Variables thatare declared within a method or a block of code).
•
ANNOTATION_TYPE – Applied only to Annotation Types.
•
PACKAGE– Applicable only to a Package.
2.3) Retention Annotation
Another commonly used Meta-data for an Annotation is the
Retention Policy
.Assume that we have some Annotations defined in thesource code. We have amechanism through which we can say that to what extent the Annotations should beretained. The three possible ways of telling this are,
•
Retain the Annotation in the Source Code only
•
Retain the Annotation in the Class file also.
•
Retain the Annotation Definition during the Run-time so that JVM can makeuse of it.The Annotation that is used to achieve this is
@Retention
and it takes a possiblevalues of
SOURCE
,
CLASS
and
RUNTIME
defined in
RetentionPolicy
Enumeration. Forexample, if we want to retain the
@TestAnnotation
information till the class file, wecan define something like this,
TestAnnotation.java
@Target(ElementType.METHOD)@Retention(RetentionPolicy.CLASS)public @interface TestAnnotation{// Property Definitions here.}
3) Built-in Annotations in Java
There are some pre-defined annotations available in the Java Programminglanguage. They are,