1. What are Java Annotations:

– Java Annotations are metadata which are adding to Java elements. An annotation indicates that the declared element should be processed in some special way by a compiler, development tool, deployment tool, or during runtime.

2. Create a Java Annotation (Annotation type) :

– Annotations types are blueprints for annotations, similar to how a class is the blueprint for an object.

– Example define a annotation:

public @interface MyAnnotation {
   String doSomething();

– Example annotate your code (Annotation):

@MyAnnotation("do something")
public void myMethod() {..}

– There are three type of annotation type:

+ Marker Annotation: doesn’t have any arguments.


public @interface MyAnnotation {}


public void mymethod() {...}

+ Single Anntation: only have one argument.


public @interface MyAnnotation {
    String doSomething();


@MyAnnotation ("What to do")
public void mymethod() {...}

+ Normal Annotation : have multiple argument.

– Example:

public @interface MyAnnotation {
   String doSomething();
   int count();


@MyAnnotation (doSomething="What to do", count=1)
public void mymethod() {...}

3. Rules of Thumb for defining annotation type:

– Annotation declaration should start with an ‘at’ sign like @, following with an interface keyword, following with the annotation name.

– Method declarations should not have any parameters.

– Method declarations should not have any throws clauses.

– Return types of the method should be one of the following:

  • primitives
  • String
  • Class
  • enum
  • array of the above types

4. Target Annotation:

– The target annotation indicates the targeted elements of a class in which the annotation type will be applicable. It contains the following enumerated types as its value:

  • @Target(ElementType.TYPE)—can be applied to any element of a class
  • @Target(ElementType.FIELD)—can be applied to a field or property
  • @Target(ElementType.METHOD)—can be applied to a method level annotation
  • @Target(ElementType.PARAMETER)—can be applied to the parameters of a method
  • @Target(ElementType.CONSTRUCTOR)—can be applied to constructors
  • @Target(ElementType.LOCAL_VARIABLE)—can be applied to local variables
  • @Target(ElementType.ANNOTATION_TYPE)—indicates that the declared type itself is an annotation type

5. The Retention annotation

– The retention annotation indicates where and how long annotations with this type are to be retained. There are three values:

  • RetentionPolicy.SOURCE—Annotations with this type will be by retained only at the source level and will be ignored by the compiler
  • RetentionPolicy.CLASS—Annotations with this type will be by retained by the compiler at compile time, but will be ignored by the VM
  • RetentionPolicy.RUNTIME—Annotations with this type will be retained by the VM so they can be read only at run-time

6. The Documented annotation

-The documented annotation indicates that an annotation with this type should be documented by the javadoc tool. By default, annotations are not included in javadoc. But if @Documented is used, it then will be processed by javadoc-like tools and the annotation type information will also be included in the generated document

* Reference from http://www.developer.com.