Introduction to Java annotation (2)

Source: Internet
Author: User

In the introduction to Java annotation (1), we have introduced in detail the role and definition of annotation and java standard annotation. This article focuses on how to customize annotation and use custom annotation.
This article does not explain the examples in detail. If you do not understand the examples, see Java annotation introduction (1 ).
Annotation is a special interface. Therefore, you can define methods and attributes in annotation, or inherit a class from annotation (implements ).

The following is a simple example. Let's learn more about Annotation step by step.

No method/attribute annotation example:

Myannotation0.java

1.PackageCom. Test. annotation;

2.

3.Public @ InterfaceMyannotation0 {

4.

5 .}

Package com. Test. annotation;

Public @ interface myannotation0 {

}

Myannotation0An Annotation without any methods or attributes.
Use myannotation0:
Testmyannotation0.java

1. @ myannotation0

2.Public ClassTestmyannotation0 {

3. @ myannotation0

4.Public VoidTestmethod (){

5.

6 .}

7 .}

@ Myannotation0

Public class testmyannotation0 {

@ Myannotation0

Public void testmethod (){

}

}

There is a value method annotation example:

Myannotation1.java

1.Public @ InterfaceMyannotation1 {

2.

3 ./**

4. * Value Method

5. * @ Return Value

6 .*/

7.PublicString Value ();

8 .}

Public @ interface myannotation1 {

/**

* Value Method

* @ Return Value

*/

Public String Value ();

}

Myannotation1Has a method named value.
Myannotation1 usage:
Testmyannotation1.java

1. @ myannotation1 ("hello ")

2.Public ClassTestmyannotation1 {

3. @ myannotation1 (value = "world ")

4.Public VoidTestmethod (){

5 .}

6 .}

@ Myannotation1 ("hello ")

Public class testmyannotation1 {

@ Myannotation1 (value = "world ")

Public void testmethod (){

}

}
You can use the @ annotation name (method name 1 = value 1, method name 2 = value 2 ,...) To assign a value to annotation. When there is only one method, the value assignment form of @ annotation name (value 1) can be omitted directly. When the method returns an array, you can use the method name = {value 1, value 2 ,...} Assign a value to it.

 

There is a value method and an annotation attribute example:

If necessary, you can also define attributes for annotation. As follows:
Myannotation2.java

1.@ InterfaceMyannotation2 {

2.PublicString Value ();

3.PublicString myproperty = "Hello World ";

4 .}

@ Interface myannotation2 {

Public String Value ();

Public String myproperty = "Hello World ";

}
Among them, myproperty can only be declared as public or non-public (public by default when there is no public modifier) is static, final attribute (even if not written, static, final by default ).

Example:
Testmyannotation2

1.ClassTestmyannotation2 {

2.Public Static VoidMain (string [] ARGs ){

3. system. Out. println (myannotation2.myproperty );

4 .}

5.

6. @ myannotation2 ("")

7.Public VoidTestmethod1 (){

8 .}

9 .}

Class testmyannotation2 {

Public static void main (string [] ARGs ){

System. Out. println (myannotation2.myproperty );

}

@ Myannotation2 ("")

Public void testmethod1 (){

}

}
Printed at the meeting:

Hello World

 

Definition and use of simplified Annotation

This section describes the definition and use of complex annotation.
First look at the Code:
Myannotation3.java

1.Public @ InterfaceMyannotation3 {

2.PublicString Value ();

3.PublicString [] multivalues ();

4.IntNumber ()Default0;

5 .}

Public @ interface myannotation3 {

Public String Value ();

Public String [] multivalues ();

Int number () default 0;

}
Myannotation3 has a multivalues method that returns string [], and a number method that returns Int. The number method has the default value 0.

Example:
Testmyannotation3.java

1.ClassTestmyannotation3 {

2. @ myannotation3 (value = "Call testmethod1", multivalues = {"1", "2"}, number = 1)

3.Public VoidTestmethod1 (){

4.

5 .}

6.

7. @ myannotation3 (value = "Call testmethod2", multivalues = {"1", "2 "})

8.Public VoidTestmethod2 (){

9.

10 .}

11 .}

Class testmyannotation3 {

@ Myannotation3 (value = "Call testmethod1", multivalues = {"1", "2"}, number = 1)

Public void testmethod1 (){

}

@ Myannotation3 (value = "Call testmethod2", multivalues = {"1", "2 "})

Public void testmethod2 (){

}

}

Number has a default value. Therefore, you can leave it unspecified when marking. Other methods must be assigned values through the method described above. Multivalues returns a string [] array, so you can assign values to it through multivalues = {"1", "2.

This article introduces how to customize annotation. We will focus on the application of annotation in future articles.

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.