Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do Java annotation like @name("Luke") with no attribute inside parenthesis?

How I can do custom Java annotation with no attribute name inside parentheses?

I don't want this: @annotation_name(att=valor). I just want like in Servlets, i.e:

@WebServlet("/main") 
like image 586
Lucas Batistussi Avatar asked Aug 02 '12 21:08

Lucas Batistussi


People also ask

Which part is necessary for custom annotation in Java?

Java Custom annotations or Java User-defined annotations are easy to create and use. The @interface element is used to declare an annotation. For example: @interface MyAnnotation{}

Are annotations necessary in Java?

Annotations are used to provide supplemental information about a program. Annotations start with '@'. Annotations do not change the action of a compiled program. Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.


1 Answers

Define the annotation with an attribute named value, then the attribute name can be omitted:

@interface CustomAnnotation {     String value(); } 

This can be used like so:

@CustomAnnotation("/main") // ... 
like image 181
pb2q Avatar answered Sep 23 '22 00:09

pb2q