[J2SE 5.0 topics] [2.7] Annotations

Source: Internet
Author: User
Annotations (Annotations) are a major new feature in J2SE 5.0. In the future, the EJB 3.0 specification will fully utilize this feature to simplify the definition and implementation of Session Bean, Message-Driven Bean, and EntityBean.
Java annotations are more or less affected by. NET. This is not to say that Java has no annotation. Previous annotations such as JavaDoc, especially @ deprecated, are annotations, but this update pushed the annotation to an unprecedented level in Java history. Through annotations, we can implement previously relatively complex advanced functions.
Let's take a look at an actual example:
/*
* Created on 2004-12-28
*
*/
Import java. lang. annotation .*;
/**
* @ Author Sean
*
*/
Public class MyAnnotations {
@ MyAnnotationForMethods (
Index = 1,
Info = "This is a method to test MyAnnotation .",
Developer = "Somebody else"
)
Public void testMethod1 (){
//...
}
@ MyEmptyAnnotation
@ MySingleElementAnnotation ("For instruction purpose only .")
@ MyAnnotationForMethods (
Index = 2,
Info = "This method is to show multiple annotations ."
)
Public void testMethod2 (){
//...
}

}
@ Target (ElementType. METHOD)
@ Interface MyAnnotationForMethods {
Int index ();
String info ();
String developer () default "Sean GAO ";
}
@ Interface MyEmptyAnnotation {
}
@ Interface MySingleElementAnnotation {
String value ();
}
In this example, I have defined three annotations: The first MyAnnotationForMethods contains three members: index, info, and developer. The developer has the default value "Sean GAO ", previous @ Target (ElementType. METHOD) is the annotation of this annotation, indicating that this annotation can only be used for methods; the second MyEmptyAnnotation is an empty annotation that can be used as a tag, such as the earlier Serializable interface; the third MySingleElementAnnotation contains a member, value, which is a rigid rule. When there is only one element, the name value must be used, it can be a value type, String, Class, enum, or an unary array of the preceding data type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.