Rules of Thumb for Defining Java Annotation Types
Here are some rules-of-thumb when defining an annotation type:
- Annotation declaration should start with an ‘at’ sign like @, following with an interface keyword, following with the annotation name.
- Method declarations should not have any parameters.
- Method declarations should not have any throws clauses.
- Return types of the method should be one of the following:
- primitives
- String
- Class
- enum
- array of the above types
No Annotation Concept : Traditional Programming
0 1 2 3 4 |
package www.technicaljungle.com; public interface NoAnnotationInterface { String doSomething(); } |
1 2 3 4 5 6 7 8 9 10 |
package www.technicaljungle.com; public class NoAnnotationSample implements NoAnnotationInterface{ @Override public String doSomething() { return "www.TechnicalJungle.com"; } } |
Java Annotation Types
There are three types of Annotation Types:
- Single Element Type
- Multi Element Type
- Marker Type
Single Element
Single-element, or single-value type, annotations provide a single piece of data only. This can be represented with a data=value pair or, simply with the value (a shortcut syntax) only, within parenthesis.
1 2 3 4 5 6 7 |
package www.technicaljungle.com; // interface is prefixed by "@". public @interface AnnotationInterface { //return type is "String". No parameter in method. String doSomething(); } |
1 2 3 4 5 6 7 |
package www.technicaljungle.com; // No need of "implements" keyword. @AnnotationInterface(doSomething="www.technicaljungle.com") public class AnnotationSample{ } |
Multi Value
Multi-element, or Multi-value type, annotations provide a multiple pieces of data. This can be represented with a data=value pair or, simply with the value (a shortcut syntax) only, within parenthesis for all possible datas.
One more use of marker interface, a marker interface called Thread Safe can be used to communicate other developers that classes implementing this marker interface gives thread-safe guarantee and any modification should not violate that.
Marker interface can also help code coverage or code review tool to find bugs based on specified behavior of marker interfaces.
Again Annotations are better choice @ThreadSafe looks lot better than implementing ThraedSafe marker interface.
1 2 3 4 5 6 |
package www.technicaljungle.com; public @interface AnnotationInterface { String doSomething(); String doSomethingNew(); } |
1 2 3 4 5 6 |
package www.technicaljungle.com; @AnnotationInterface(doSomething="www.technicaljungle.com", doSomethingNew = "CodeSnippet") public class AnnotationSample{ } |
Marker Type
Marker-element, or Multi-value type, annotations is useful when there is no method or data resides in the interface. Benefit of marker interface is “making a class Serializable or Clonnable”.
1 2 3 4 |
package www.technicaljungle.com; public @interface MarkerAnnotationInterface { } |
1 2 3 4 5 6 |
package www.technicaljungle.com; @MarkerAnnotationInterface public class MarkerAnnotationSample{ } |
@Annotations