Click here to Skip to main content
12,549,996 members (50,677 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked

Understanding Annotations in Java

, 23 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This article explains how annotations work in Java.

Sample Image


This article discusses the way of creating annotations in Java. It also shows how to apply annotations to other declarations. Finally it discusses how to obtain annotation information at runtime using Reflection.


Annotation is a new feature introduced by J2SE 5 that allows programmers to embed additional information called metadata into a Java source file. Annotations do not alter the execution of a program but the information embedded using annotations can be used by various tools during development and deployment.

Using the code

Creating an annotation is similar to creating an interface. But the annotation declaration is preceded by an @ symbol. The annotation declaration itself is annotated with the @Retention annotation. The @Retention annotation is used to specify the retention policy, which can be SOURCE, CLASS, or RUNTIME.

  • RetentionPolicy.SOURCE retains an annotation only in the source file and discards it during compilation.
  • RetentionPolicy.CLASS stores the annotation in the .class file but does not make it available during runtime.
  • RetentionPolicy.RUNTIME stores the annotation in the .class file and also makes it available during runtime.
// Specifying runtime retention policy
@interface MyAnnotation
    String author();    // Annotation member
    String date();      // Annotation member

An annotation cannot have the extends clause. However, annotations implicitly extend the Annotation interface. The body of an annotation consists of method declarations without body. These methods work like fields.

In the above example, I have created two members, author and date to represent information about the creator, and date of writing of the class and method.

Once an annotation is created, it can be applied to classes, methods, fields, parameters, enums, and so on. While applying an annotation to a declaration, you must provide values for its members as follows:

// Applying annotation to the class
public class Test
    // Applying annotation to the method
    public static void testMethod()
        System.out.println("Welcome to Java");
        System.out.println("This is an example of Annotations");
    public static void main(String args[])

Annotations can be queried at runtime using Reflection as follows:

public static void showAnnotations()
// Function to show annotation information
    Test test=new Test(); // Instantiating Test class
        Class c=test.getClass(); // Getting Class reference
        Method m=c.getMethod("testMethod"); // Getting Method reference
        // Getting Class annotation
        MyAnnotation annotation1=
        // Getting Method annotation
        MyAnnotation annotation2=m.getAnnotation(MyAnnotation.class);
        // Displaying annotation information
        System.out.println("Author of the class: ";
        // Displaying annotation information
        System.out.println("Date of Writing the class: ";
        // Displaying annotation information
        System.out.println("Author of the method: ";
        // Displaying annotation information
        System.out.println("Date of Writing the method: ";
    catch(NoSuchMethodException ex)
        System.out.println("Invalid Method..."+ex.getMessage());

In the above code, I have queried annotations applied to the class Test as well as the method testMethod().

To obtain annotation information for the class, the following statement is used:

MyAnnotation annotation1=(MyAnnotation)c.getAnnotation(MyAnnotation.class);

To obtain annotation information for the method, the following statement is used:

MyAnnotation annotation2=m.getAnnotation(MyAnnotation.class);

The annotation information is printed by using annotation objects.

Points of interest

The Java program is compiled on the command line as follows:


and executed as follows:

java Test


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Azim Zahir
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with NIIT (Mumbai, India) as a Senior Faculty. I enjoy programming as a hobby. My favorite technologies are Flash, Flex and Silverlight.

Of late I have developed keen interest in WPF and Windows Mobile programming.

Apart from computers, my favorite pastime is bicycling.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Member 115926328-Apr-15 21:27
memberMember 115926328-Apr-15 21:27 
GeneralMy vote of 5 Pin
Member 112107805-Nov-14 20:44
memberMember 112107805-Nov-14 20:44 
GeneralMy vote of 5 Pin
Mustanseer Sadarwala13-Dec-12 21:43
memberMustanseer Sadarwala13-Dec-12 21:43 
GeneralRe: My vote of 5 Pin
Azim Zahir21-Dec-12 19:23
memberAzim Zahir21-Dec-12 19:23 
GeneralRe: My vote of 5 Pin
paulsundarsinghasdf6-Mar-13 23:05
memberpaulsundarsinghasdf6-Mar-13 23:05 
GeneralRe: My vote of 5 Pin
Azim Zahir11-Mar-13 19:13
memberAzim Zahir11-Mar-13 19:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161021.1 | Last Updated 23 Oct 2011
Article Copyright 2011 by Azim Zahir
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid