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

Annotations in Java: Declaring Annotations (Part-2)


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

Annotations in Java: Declaring Annotations (Part-2)


In previous post, we have seen how to declare marker and single value annotations. So, let’s continue the same example and learn how to declare multi value annotations and set default values.

Multi-value Annotations

Now let’s add a few more data members to our Task annotation:

@interface Task {

          String description();

          String targetDate();

          int estimatedHours();

           String additionalNote();

}

 

 

·         The description member instructs the developer about the nature of the task.

·         The targetDate member sets the expected deadline.

·         The estimatedHours member specifies the number of man-hours required to complete the job,

·         and the additionalNote member may be used to specify any additional instructions to the developer.

We can now annotate our method as follows:

@Task(description = "Implement tax computations",

targetDate = "Jan 1, 2012",

estimatedHours = 50,

additionalNote = "This implementation is critical for the final launch")

 

 

See how all four members are specified using data=value syntax? These types of annotations are called multi-value annotations.

Setting Default Values

We are now allowed to specify the default values for any of the data members. We do so by using the default keyword. For example, in the modified definition of Task shown here, we have set the default targetDate:

@interface Task {

 

         String description();

         String targetDate() default "Jan 1, 2012";

         int estimatedHours();

         String additionalNote();

}

 

 

When we annotate our code using this modified Task annotation, we need not specify the targetDate member unless we want to assign a different value to it.

Custom Annotation Program

The full program that contains the concepts discussed thus far is given here

public class CustomAnnotation {

             @WorkInProgress

             @Task(description = "Implement tax computations", targetDate = "Jan 1, 2012",

                 estimatedHours = 50, additionalNote = "This implementation is critical for the

                 final launch")

                    

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

                             return 0;

               }

}

 

@interface WorkInProgress {

}

 

@interface Task {

                     String description();

 

                     String targetDate();

 

                     int estimatedHours();

 

                     String additionalNote();

}

 

Rules for Defining Annotation Types

To summarize our discussion about creating annotations, here are the rules for defining annotation types:

·         An annotation declaration starts with @interface, followed by the annotations name.

·         To create parameters for an annotation, you declare methods in its type.

·         Method declarations should not contain any parameters.

·         Method declarations should not contain any throws clauses.

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

o    Primitive

o    String

o    Class

o    Enum

o    An array of the preceding types


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

Follow MindStick