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 @CodeDescription 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.

Computer code Code listing 1.1: Annotation type declaration
@interface CodeDescription
{
    String author;
    String version;
}

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 interface 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:

Computer code Code listing 1.2: Calling of annotation
public class MyMethod
{
    @CodeDescription
    (
        author = "Unknown",
        version = "1.0.0.1"
    )
    public void doSomething
    {
        ...
    }
}

Note:
Instead of using the declaration with the class, the annotation is used with the method doSomething. This might not demonstrate the power of annotations yet, but more will be explored in later chapters.

Using a default value

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

Computer code Code listing 1.3: Using default values.
@interface CodeDescription
{
    String author;
    String version default "1.0.0.1";
}

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

Computer code Code listing 1.4: Pre-defined value.
public class MyMethod
{
    @CodeDescription(author = "Sysop")
    public void doSomething
    {
        ...
    }
}


Clipboard

To do:
Add some exercises like the ones in Variables


  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.

Java_Programming/Annotations/Custom_Annotations
 



 

Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.


Manage research, learning and skills at defaultlogic.com. Create an account using LinkedIn to manage and organize your omni-channel knowledge. defaultlogic.com is like a shopping cart for information -- helping you to save, discuss and share.


  Contact Us