Tuesday, February 16, 2010

Java Annotations

  • provides data about a program but do not directly affect the program semantics

  • defined similar to interface
    @interface Author {
    String author();
    String date();
    String[] reviewers(); // Note use of array
    }

  • sample use:
    @Author(
    name = "Benjamin Franklin",
    date = "3/27/2003",
    reviewers = {"Alice", "Bob", "Cindy"} // Note array notation
    )
    class MyClass() { }


3 Predefined Annotation Types:
1) @Deprecated - should also be documented using the Javadoc @deprecated tag
/**
* @deprecated
* explanation of why it was deprecated
*/
@Deprecated

2) @Override

3) @SuppressWarnings
@SuppressWarnings("deprecation") //deprecation warning - suppressed
@SuppressWarnings({"unchecked", "deprecation"}) // legacy code & deprecation warning - suppressed

Detailed explanation here: http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html

No comments:

Post a Comment