Magazine
 
Java Developers Desk: Annotations

Usage:
@Example (“Hi ! How r you”)
public void anymethod(){
————
}

Multi-value or Full-value

These types of annotations can have multiple data members. Therefore use full value annotations to pass the values to all the data members.

Example:

public @interface Example{
String showSomething();
int num;
String name;
}

Usage:

@Example (showSomething = “Hi! How r
you”, num=5, name=”amit” )
public void anymethod{
// code here
}

Rules defining the Annotation type:

Here are some rules that one should follow while defining and using annotations types

  • Start the annotation declaration starting with the symbol “at” @ following the interface keyword that should follow the annotation name.
  • Method declaration should not throw any exception.
  • Method declaration should not contain any parameter.
  • Method using annotations should return a value, one of the types given below:
  • String
  • primitive
  • enum
  • Class
  • array of the above types

 

 

Categorizing Annotations:
JDK 5 contains two categories of annotations:

Simple annotations:
These types of annotations are used to annotate the code only. We can not use these types of annotations for creating the custom annotation type.

Meta annotations:
Also known as annotations of annotations are used to annotate the annotation-type declaration.

I. Simple annotations:
JDK 5 includes three types of simple annotations.

  • Override
  • Depricated
  • Suppresswarning

JDK 5 does not include many built-in annotations but it facilitates to core java to support annotation features. Now will discuss in brief each of the above simple annotation types along with examples.

1) Override annotation:

The override annotation ensures that the annotated method is used to override the method in the super class. If the method containing this type of annotation does not
override the method in the super class then the compiler will generate a compile time error. Lets take an example and demonstrate what will happen if the annotated method does not override the method in the super class.

Example 1:

public class Override_method{
@Override
public String toString(){
return super.toString() +
“Will generate an compile time error.”;
}}
Jan 2008 | Java Jazz Up | 8
 
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