Annotation Programming Basics

Rules of Thumb for Defining Java Annotation Types

Here are some rules-of-thumb when defining an annotation type:

  1. Annotation declaration should start with an ‘at’ sign like @, following with an interface keyword, following with the annotation name.
  2. Method declarations should not have any parameters.
  3. Method declarations should not have any throws clauses.
  4. Return types of the method should be one of the following:
    • primitives
    • String
    • Class
    • enum
    • array of the above types

No Annotation Concept : Traditional Programming

 


Java Annotation Types

There are three types of Annotation Types:

  1. Single Element Type
  2. Multi Element Type
  3. Marker Type

 

Single Element

Single-element, or single-value type, annotations provide a single piece of data only. This can be represented with a data=value pair or, simply with the value (a shortcut syntax) only, within parenthesis.

 


Multi Value

Multi-element, or Multi-value type, annotations provide a multiple pieces of data. This can be represented with a data=value pair or, simply with the value (a shortcut syntax) only, within parenthesis for all possible datas.

One more use of marker interface, a marker interface called Thread Safe can be used to communicate other developers that classes implementing this marker interface gives thread-safe guarantee and any modification should not violate that.

Marker interface can also help code coverage or code review tool to find bugs based on specified behavior of marker interfaces.

Again Annotations are better choice @ThreadSafe looks lot better than implementing ThraedSafe marker interface.

 

 

 


Marker Type

Marker-element, or Multi-value type, annotations is useful when there is no method or data resides in the interface. Benefit of marker interface is  “making a class Serializable or Clonnable”.

 

 

 

 

@Annotations

  1. Annotations and Its Benefits in Java
  2. Advantages Of Annotations Over XML
  3. Annotation Programming Basics
  4. Annotation Types
    1. Simple Annotation
    2. Meta Annotation
      1. Meta ‘Target’ Annotation
      2. Meta ‘Retention’ Annotation
      3. Meta ‘Documented’ Annotation
      4. Meta ‘Inherited’ Annotation
  5. Reflection to Extract Annotation Details

Leave a Reply

Your email address will not be published. Required fields are marked *