Magazine
 
Java Developers Desk: Annotations

@Test_Target(doTestElement=”Hi ! How r
you”)
^
Error in javac compilation

2) Retention annotation:

These types of annotation specify where and how long annotation with this types are to be retained. There are three type of Retention annotations are of three types.

  • RetentionPolicy.SOURCE: This type of
    annotation will be retained only at
    source level and the compiler will ignore
    them.
  • RetentionPolicy.CLASS: This type of
    annotation will be retained at the
    compile time the virtual machine (VM) will
    ignore them.
  • RetentionPolicy.RUNTIME: Virtual
    machine will retained the annotation of
    this type and they can be read only at
    run-time.
  • Lets demonstrate that how this type of
    annotations are applied by taking an
    example using RetentionPolicy.RUNTIME.

Example:

@Retention(RetentionPolicy.RUNTIME)
public @interface Retention_Demo {
String doRetentionDemo();
}

This example uses the annotation type @Retention(RetentionPolicy.RUNTIME) that indicates the VM will retained your Retention_Demo annotation so that it can be read effectively at run-time.

3) Documented annotation:

This type of annotation should be documented by the javadoc tool. javadoc does not include the annotation by default. Include the

 

annotation type information by using @Documented in the generated document. In this type of annotation all the processing is done by javadoc-like tool.

The given example demonstrates the use of the @Documented annotations.

Example:

@Documented
public @interface Documented_Demo {
String doTestDocumentedDemo();
}

Next, make changes in Test_Annotations class as follows:

public class Test_Annotations {
public static void main(String arg[]) {
new
Test_Annotations().doTestRetentionDemo();
new
Test_Annotations().doTestDocumentedDemo();
}
@Retention_Demo
(doTestRetentionDemo=”Hello retention
annotation”)
public void doTestRetentionDemo() {
System.out.printf(“Testing ‘Retention’
annotation”);
}
@Documented_Demo
(doTestDocumentedDemo=”Hello Test
documentation”)
public void doTestDocumentedDemo() {
System.out.printf(“Testing ‘Documented’
annotation”);
}}

4) Inherited Annotation:

This annotation is little bit complex. It inherits the annotated class automatically. If you specify @Inherited tag before defining a class then apply the annotation at your class and finally extend the class then the child class inherits the properties of the parent class automatically. Lets demonstrate the benefits of using the

Jan 2008 | Java Jazz Up | 11
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

83, 84 , 85 , 86, 87 , 88, 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 , 101 , 102 , 103, 104 , 105 ,

106, 107,

Download PDF