Defining your own Annotations

You can use the @Interface annotation to create your own annotation definition.

Procedure

Use the @Interface annotation to define your own annotation definition:
  • Annotation definitions resemble interface definitions
  • Annotation method declarations have neither parameters nor throws clauses, and return one of the following elements:
    • primitives
    • String
    • Class
    • enum
    • array of the above types
  • Methods can have default values
public @interface CreatedBy{
     String name();
     String date();
     boolean contractor() default false;
}          
@CreatedBy(name = "Mary Smith",date="02/02/2008");
public class MyClass{....}         

Results

Meta-annotations: Meta-annotations (annotations of annotations) provide additional information about how an annotation can be used: Other built-in annotations:

Feedback