Java J2EE Portal
Enterprise Java Station
J2EE curve
Java News / Articles
Java News / Articles
Ensuring Message Integrity Using Java and Message Digests
Task Scheduling with Quartz - Integration with OSWorkflow
Enterprise Mashups – Opportunities and Challenges!
Processing...
Buy Java, Deals On Software Technology Store
Click here for great deals on computers, laptops, software and books
What's new in J2SE 5.0? Part 3: MetaData / Annotations and Enumerated Types PDF Print
Written by Chinmay Ogale   
Feb 16, 2005 at 01:58 PM

In the first two parts of this series of articles, we had a look at generics, enhanced for loop and auto boxing/ unboxing. In this final article in this series, we will look at metadata and annotations. What's new in J2SE 5.0? Part 3: MetaData / Annotations and Enumerated Types

Three part series: What's new in J2SE 5.0?:

---------------------------------------------------------------------------------------------------------

What's new in J2SE 5.0?- Part 3


MetaData / Annotations

Today, annotations and metadata are the buzzwords in the world of Java. J2SE 5.0 has brought this feature for us. In simple words metadata means data about data.

Javadoc comments are widely used to document the code. Then, why are annotations required?

Annotations go well beyond what Javadoc is capable of and achieve compile-time checking and analysis of Java code. This means we can perform many checks on compile-time itself, significantly reducing the debugging time spent trying to fix runtime errors.. For example, we override methods form the super class. And by using @Override annotation the information about method overriding is provided.

Annotations can be categorized as follows:

  • Marker annotations: These annotations do not use any additional data. So no variables are used and just name is used.
  • Single-value annotations: In this case, data supplied is just one argument or a single data member.
  • Full annotations: More than one data elements are used by these types of annotations.

Listing 1 shows how the annotations are declared.

Listing 1

public class MyAnnotate {

@Override

public String toSring() {

return super.toString() + " Annotation Override";

}

}

In Listing 1 the annotation @Override has been used... Override is an in-built annotation provided with J2SE 5.0. We will use this annotation as an example.

  • Now let?s see how annotations help us in providing data/information about our code and how at compile-time the code is checked with the help of annotations.

Listing 2

Compiling 1 source file to C: JavaApplication1buildclasses
C:JavaApplication1srcjavaapplication1MyAnnotate.java:3: method does not override a method from its superclass
@Override
1 error
BUILD FAILED (total time: 1 minute 11 seconds)

When we define the override annotation and compile, we get an error as shown in Listing 2. This error clearly states that the method we are trying to override doesn?t exist in the super class. Thus this annotation notifies that the method, which is annotated, is actually overriding the method from superclass. This annotation is used on methods and not on classes, interfaces or package declarations.

In J2SE 5.0 one more built-in annotation Deprecated have been provided. You can also define your own custom annotations.




Add This Feed Button

Enter your Email


Java Expert Interviews
PatrickLightBodyWebWork
Struts Action Framework 2.0 should be released by August 2006
JavaFX Expert
JavaFX Is Many Times Faster And Easier Than Swing
Ramesh Loganathan Pramati
Pramati 4.1 and beyond: An interview with Ramesh Loganathan
Processing...
Go to top of page  Home |
SiteMap

Copyright 2004 to 2008 Rightrix Solutions. All rights reserved. All product names are trademarks of their respective companies. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Rightrix Solutions and IndicThreads.com are independent of Sun Microsystems, Inc.

Views expressed at IndicThreads.com reflect the views of the authors alone, and do not necessarily reflect those of IndicThreads.com. IndicThreads.com and it's authors are not responsible for reader comments and opinions.

Enterprise Java J2EE JEE Portal >> IndicThreads.com