December 19, 2022How To Create Custom Annotation In JavaCreate Custom Annotation:In java, creating an annotation is @interface is used to create an Annotation.public @interface MyAnnotation{ }We can also define methods inside an annotation.public @interface MyAnnotation{ int value(); }Note: The methods of an annotation should adhere to the following rules:Method declaration should not have any parametersMethod declaration should not have any throws clauseMethod return type should be of primitives, String, Class, Enum, Annotations, and Arrays of the preceding typesAlso, we can provide a default value for the methods inside an annotation.public @interface MyAnnotation{ int value() default 1; }Types of Annotations:Marker AnnotationSingle – Value AnnotationMulti – Value AnnotationMarker Annotation:An annotation without any methods is called as Marker Annotation.public @interface MyAnnotation{ }@Override and @Deprecated are the best examples of marker annotation.Single Value Annotation:An annotation with only one method is called as single – Value Annotation.public @interface MyAnnotation{ int value(); }Multi Value Annotation:An annotation with more than one method is called as Multi – Value Annotation.public @interface MyAnnotation{ int value1(); String value2(); }The following are the built-in annotations that can be used for Custom Annotations:@Target@Retention@Inherited@Documented@TargetIt is used to specify on which type the annotation is to be applied.The java.lang.annotation.ElementType enum provides many constants to specify the type of element where annotation is to be applied. Here’s the list of constants of ElementTypoe enum:Element TypeElement TypeTYPEclass, interface or enumerationFIELDfieldsMETHODmethodsCONSTRUCTORconstructorsLOCAL_VARIABLElocal variablesANNOTATION_TYPEannotation typePARAMETERparameter@RetentionIt is used to specify on which level annotation will be available.Retention PolicyAvailabilityRetentionPolicy.SOURCEIt is used to make the annotation to refer only to the source code and it will be discarded during compilation. So, it will not be available in the compiled .class file.RetentionPolicy.CLASSIt is used to make the annotation available to java compiler, but not to JVM. So, it is included in the compiled .class file.RetentionPolicy.RUNTIMEIt is used to make the annotation available at run-time. So it is included in the compiled .class file and at run-time.@InheritedBy default, annotations are not inherited to subclasses. The @Inherited annotation marks the annotation to be inherited to subclasses.@DocumentedIt is used to mark the annotation to be included in the documentation.Now let us see an example how to Create and Apply a Custom Annotation.@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation{ int value1() default 0; String value2() default "J2EEKART"; boolean value3() default false; }class AnnotationTest{ @MyAnnotation(value1 = 10, value2 = "Hi", value3 = true) public void display(){ } }import java.lang.reflect.Method; class Test{ Test obj= new Test(); Method m = obj.getClass().getMethod("display"); MyAnnotation annot = m.getAnnotation(MyAnnotation.class); System.out.println(annot.value1()); System.out.println(annot.value2()); System.out.println(annot.value3()); }The output of the above example will be as:10 Hi trueThe above example shows how to create a Custom Annotation with default values and in-built annotations, apply an annotation on method level and read the values of an annotation by using java reflection. Like this:Like Loading...Related Java annotationDocumentedhow to create annotation in javainheritedjavamarket annotationmulti value annotationretentionsingle value annotationtarget