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

Posted

in

by

Tags:


Related Posts

Comments

One response to “Introduction to Annotation in JAVA”

  1. […] Meta Annotation – annotate other annotation Posted by Jitendra Zaa on March 16th, 2011 In Previous article, we have seen that what are the types of annotation provided by the JAVA. […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading