Introduction to Annotation in JAVA

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 1 Comment on Introduction to Annotation in JAVA

Introduction to Annotation in JAVA

Annotation is the powerful feature provided by JAVA in version 5 (Tiger). It provides Data about the program to the compiler.
By Convention, Annotation comes first on its line.
Example:

/**
 *
 * @author jitendra Zaa
 * @version 1.0.5
 *
 */
public class TestAnnotations

If there is just one element named “value,” then the name may be omitted, as in:

@SuppressWarnings("unchecked")
public void display()

Also, if an annotation has no elements, the parentheses may be omitted, as in:

@Override
public void display()

Annotations used by the Java Compiler:

There are basically three types of the annotations used by the Java Compiler:

@Deprecated :
The @Deprecated annotations indicates compiler that the marked class, method or field is deprecated and should not be used. Compiler will generate warning message for that element. In eclipse it is displayed by the strikethrough font, indicating that the element is depreacted. While commenting the deprecated element its javadoc comment should also be start with symbole “@”.Also, note that the Javadoc tag starts with a lowercase “d” and the annotation starts with an uppercase “D”.

// Javadoc comment follows
    /**
     * @deprecated
     * It should not be used, it causes performance issue, better method is
     * added.
     */
    @Deprecated
    static void deprecatedMethod() { }
}

@Override:
This annotation means that the element is intended to override the superclass element. The advantage of using this annotation is that it will avoid the any typing error (Spelling Mistakes) while overriding the method. If method marked with this annotation does not override the super class method then compiler generates an error.
Example:

@Override
public void display()

@SuppressWarning :
This annotation specifies compiler to suppress the particular warning.
Example:

@SuppressWarnings("deprecation")
    void display() {
        ...
    }

Every compiler warning belongs to a category. The Java Language Specification lists two categories: “deprecation” and “unchecked”.

To suppress more than one warning, use:

@SuppressWarnings({"unchecked", "deprecation"})

Java Documentation and Tutorial:
http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html

Related posts

One thought on “Introduction to Annotation in JAVA”

Leave a Reply

Your email address will not be published. Required fields are marked *