Java Programming/Annotations/Custom Annotations

Annotations can be viewed as a source of defining meta-data for a piece of code in Java. The annotation  used in the following sections does not come as a part of the Java API.

Annotation Type Declaration
Before you can use an annotation with classes, theirs members and statements or expressions, you need to define an annotation type. Following is the syntax on how to define a type for the mentioned annotation.

That's it! Our first ever annotation has been defined. Now, we can use it with any of our classes. An annotation definition if you look closely resembles the definition of a normal interface, except that the  keyword is preceded by the   character. Some refer to this syntactical declaration as the annotation type declaration due to the fact that @ is 'AT' or 'Annotation Type' for that very instance.

Annotation Element Declarations
What look like methods in the body of the annotation definition are called annotation element declarations. These are the named entities that we used with the annotation body in the example in the previous section. However, for the sake of clarity, code below also represents the calling of the following annotation:

Using a default value
Now, for instance, you want the annotation to know that if no value for the  element is present, then it should use a default value. Declaring a default value would be done the following way.

So, now if you use the same code again, you can ignore the  element because you know that the value is to be provided by default.