Defining own annotation type
by , 10-31-2011 at 05:49 PM (846 Views)
The Java language supports defining new annotation types. An annotation type looks similar to an ordinary class, but it has some unique properties. One can use it with the at sign in the classes to annotate your other Java code.
Its right to say that defining a new annotation type is similar to creating an interface. We have to precede the interface keyword with the @ sign.
The following example shows the simplest possible annotation type:
Compile this annotation type and put it in your classpath. Now the annotation is ready to be used in the source code methods.Java Code:package com.domain.tiger.a; /** * Marker annotation to indicate that a method or class * is still in progress. */ public @interface InProgress { }
I will use the inProgress annotation declared above.
The following example uses the declared custom annotation.
Thing to note is that we use the custom annotation type exactly the same way as we use the built-in annotation types. Only difference is that we indicate the custom annotation by both its name and package.Java Code:@com.domain.tiger.a.InProgress public void calculateInterest(float amount, float rate) { // Need to finish this method later }
You may import the annotation type and refer to it as simply @InProgress.









Email Blog Entry
PDF to TIFF Conversion & Control...
Yesterday, 11:39 AM in Java Software