Monday, February 3, 2014

How to create basic custom annotation?

  • Annotations are created by using @ sign, folled by the keyword interface, and followed by annotation name as shown in the below example.
  • Members can be decleared as shown in the example, it looks like methods. The example defines two members called name and desc. We should not provide implementation for these members.
  • All annotations extends java.lang.annotation.Annotation interface. Annotations cannot include any extends caluse.
  • Below example shows how to use this annotation to method.
Example :
package com.java2novice.annotations;
 
public @interface MySampleAnn {
 
    String name();
    String desc();
}
 
class MyAnnTest{
     
    @MySampleAnn(name = "test1", desc = "testing annotations")
    public void myTestMethod(){
        //method implementation
    }
}

No comments:

Post a Comment