Home > DeveloperSection > Articles > Annotations in Java: Declaring Annotations (Part-1)

Annotations in Java: Declaring Annotations (Part-1)

Java Java 
0 Comment(s)
 201  View(s)
Rate this:

Annotations in Java: Declaring Annotations (Part-1)

Having seen the built-in simple annotations in java in the previous posts, it is time now to create our own annotations. Why would we create our own annotations?

As we must have noticed, the built-in annotations allow us to do so little that, for all practical purposes, we would want to create our own. In the following section, we learn the entire process of creating our own annotations and see why doing so is important. We also learn how to annotate our annotations.

To facilitate the creation of a custom annotation, Java language has added a new type—the annotation type. It looks like an ordinary class, but it has some unique properties. The definition looks like an interface definition, except that the interface keyword is preceded by an @ sign. The annotations can be of three different types:

·         one without any elements

·         one with a single element, and

·         one with multiple elements.

 We will learn how to create all three types of annotations in this section. First, we’ll start with a simple case of an annotation with no members.

Marker Annotations

This type of annotation does not have any elements. The following statement illustrates how to declare such an annotation:

public @interface WorkInProgress {}

To use this in code, we would use the following syntax:


public static float computeTax (float amount, float rate) {

        // to be implemented

        return 0;




The @WorkInProgress annotation would probably be used to indicate to our fellow developers that the annotated method is yet to be implemented.

Single-Value Annotations

At this point, we might want to add what is yet to be done in the method’s implementation. For this, we’ll create another annotation, called Task, as shown here:

@interface Task {

          String value();




We can now annotate the computeTax method as follows:

@Task("Implement tax computations")

These types of annotations are called single-element annotations because they take only a single-value type. (We also have multivalue types of annotations that have multiple data members.)

In case of a single-element annotation, the data member is specified with the word value. The syntax for specifying a member is similar to declaring a method. A few restrictions apply to member declarations—these are discussed in next post. For now, let’s see what happens if we use any word other than value in the preceding definition. The new definition looks like this:

@interface Task {

             String description();




We now need to annotate our method using the following syntax:

@Task(description = "Implement tax computations")

Note that this time, we had to explicitly spell out the member name—description. In the earlier case, where we used the default name value, we specified only the target string, omitting the member name. If we don’t do so, the compiler will generate an error during compilation.

Next, we see multi-valued annotations in the next part.

Don't want to miss updates? Please click the below button!

Follow MindStick