Rethinking the Datacenter Sponsored by HP
Today's datacenters need to increase utilization, get control over power and cooling costs, and align with business objectives. Download this eBook to learn about the challenges facing the data center in a world where digital information is growing at a torrid pace and costs are being held in check. Learn more. »
Putting the Green into IT Sponsored by HP
Electricity use in data centers is skyrocketing, sending energy bills through the roof, creating environmental concerns and generating negative publicity. "Going Green" means looking to technologies like virtualization, energy-efficient chips and racks, and implementing policies that extend beyond the data center. Learn more. »
Managing the Modern Network Sponsored by HP
In a global economy where information crosses the globe in an instant, and where Web-based applications power business, it's more important than ever to ensure your network is safe from threats and optimized to deliver the data your business needs. »
Evaluating Software as a Service for Your Business
Sponsored by Webroot
Is Software as a Service just hype, or is something really going on here? See if your company can benefit as SaaS tries to change the face of the enterprise.
»
Is Your Disaster Recovery Plan Good Enough? Sponsored by HP
Preparing for a disaster is more often than not part of the storage planning process, and it is one of the most difficult tasks, since it includes local hardware and software, networking equipment, and a test plan. Learn how to get disaster recovery right. »
The objective of changing older JDK versions to JDK5 largely centered on enhancing ease-of-development. In other words, the new features shift the responsibility for writing the boilerplate code from the programmer to the compiler. If the source code is boilerplate free, it becomes easier to maintain. The resulting codes are also less likely to be bug-prone. One of these new ease-of-development features in JDK5 are annotations. Annotations are like meta-tags that you can add to your code and apply them to package declarations, type declarations, constructors, methods, fields, parameters, and variables. As a result, you will have helpful ways to indicate whether your methods are dependent on other methods, whether they are incomplete, whether your classes have references to other classes, and so on.
Annotation-based development is certainly one of the latest Java development trends. Annotation-based development relieves Java developers from the pain of cumbersome configuration. Quoting from Sun's official site, "It (annotation-based development) lets us avoid writing boilerplate code under many circumstances by enabling tools to generate it from annotations in the source code. This leads to a declarative programming style where the programmer says what should be done and tools emit the code to do it." Simply speaking, annotation is a mechanism for associating a meta-tag with program elements and allowing the compiler or the VM to extract program behaviors from these annotated elements and generate interdependent codes when necessary. In the first part of this three-article series, I'll describe some basics of annotation, their benefits, as well as some example usages.
The Basics
There are two things you need to consider with annotations. One is the "annotation" itself; another is the "annotation type." An annotation is the meta-tag that you will use in your code to give it some life. Annotation type is used for defining an annotation. You will use it when you want to create your own custom annotation. The type is the actual construct used, and the annotation is the specific usage of that type.
An annotation type definition takes an "at" (@) sign, followed by the interface keyword plus the annotation name. On the other hand, an annotation takes the form of an "at" sign (@), followed by the annotation type. This is simplest form of annotation. Additionally, you can put data within parenthesis after the annotation name. An example of each can be seen below:
Example to Define an Annotation (Annotation type)
public @interface MyAnnotation {
String doSomething();
}
Example to Annotate Your Code (Annotation)
MyAnnotation (doSomething="What to do")
public void mymethod() {
....
}
Annotation Types
There are three annotation types:
Marker: Marker type annotations have no elements, except the annotation name itself.
Example:
public @interface MyAnnotation {
}
Usage:
@MyAnnotation
public void mymethod() {
....
}
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.
Example:
public @interface MyAnnotation
{
String doSomething();
}
Usage:
@MyAnnotation ("What to do")
public void mymethod() {
....
}
Full-value or multi-value: Full-value type annotations have multiple data members. Therefore, you must use a full data=value parameter syntax for each member.
Example:
public @interface MyAnnotation {
String doSomething();
int count; String date();
}
Usage:
@MyAnnotation (doSomething="What to do", count=1,
date="09-09-2005")
public void mymethod() {
....
}
Rules of Thumb for Defining Annotation Type
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
Annotations
There are two types of annotations available with JDK5:
Simple annotations: These are the basic types supplied with Tiger, which you can use to annotate your code only; you cannot use those to create a custom annotation type.
Meta annotations: These are the annotation types designed for annotating annotation-type declarations. Simply speaking, these are called the annotations-of-annotations.
Add www.developer.com to your favorites Add www.developer.com to your browser search box IE 7 | Firefox 2.0 | Firefox 1.5.xReceive news via our XML/RSS feed