DEV Community

Rajesh Mishra
Rajesh Mishra

Posted on

How To Create Custom Annotation In Java

Create 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 parameters
  • Method declaration should not have any throws clause
  • Method return type should be of primitives, String, Class, Enum, Annotations, and Arrays of the preceding types

Also, we can provide a default value for the methods inside an annotation.

public @interface MyAnnotation{
   int value() default 1;
}

Types of Annotations:

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay