Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javatutorial Annotations in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Annotations in Java

Advertisement
Annotations behave like metadata and provide data about a program that is not part of the program itself. They are used to associate program elements with the meta tags so that the compiler can generate interdependent code to support the annotated elements. Developing annotations helps in faster development of program. Multi-value annotations are used to pass the values to multiple data members.

Annotations behave like metadata and provide data about a program that is not part of the program itself. They are used to associate program elements with the meta tags so that the compiler can generate interdependent code to support the annotated elements.

Developing Annotations helps in faster development of program.

Use of Annotations:

  • Compiler can use Annotations to detect errors or suppress warnings.
  • Software tools can process annotation information to generate code, XML files, and can also apply them to variables, parameters, fields type declarations, methods and constructors.
  • There are some Annotations that are available to be examined at runtime.

There are 3 types of Annotation in Java:

  • Marker
  • Single-Value
  • Multi-value/Full-value

Marker

Marker annotations do not contain any elements (except name). For example:

public @interface Example{

   }

It can be used as:

  @Example
   public void anymethod() {
  ------------
   }

Single-value

Single value annotations can be represented with the data and value pair or can be represented just by using the value only.

For Example:

public @interface Example{

  String showSomething();

   }

It can be used as:

   @Example ("Hi ! How r you")
   public void anymethod(){

   --------

   }

Multi-value/Full-value:

Multi-value annotations are used to pass the values to multiple data members.

For Example:

public @interface Example{

String showSomething();
int num; 
String name;

}

It can be used as: 

@Example (showSomething = "Hi! How r you", num=5, name="zulfiqar" )
public void anymethod{

// code here

}

While defining the annotation programmer must follow these rules:

Declare the annotation using @ followed by the interface keyword followed by the annotation name.

In an annotation method declaration should not throw any exception and should not contain any parameter.

JDK 5 (aka Tiger) contains 2 types of annotations:

  1. Simple annotations
  2. Meta annotations

There are three types of Simple annotations:

  • Override
  • Depricated
  • Suppresswarnings

There are four types of Meta annotations:

  • Target
  • Retention
  • Documented
  • Inherited
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 24, 2013

Related Tutorials

Discuss: Annotations in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com