Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make a Java annotation's value mandatory?

People also ask

What is @interface annotation in Java?

@interface is used to create your own (custom) Java annotations. Annotations are defined in their own file, just like a Java class or interface. Here is custom Java annotation example: @interface MyAnnotation { String value(); String name(); int age(); String[] newNames(); }

How do you use an array constant in annotation?

if the compiler expects an "Array Initializer" to be passed to the Annotation, declaring a compile-time constant like private static final String[] AB = { ... }; should do. it's understood that Annotation processing happens before the actual compilation, but then the error message is not accurate.

What does annotation mean in Java?

Java annotations are metadata (data about data) for our program source code. They provide additional information about the program to the compiler but are not part of the program itself. These annotations do not affect the execution of the compiled program. Annotations start with @ .


If you do not specify a default value, it is mandatory. For your example using your annotation without using the MyValue attribute generates this compiler error:

annotation MyAnnotation is missing MyValue


Given

public @interface MyAnnotation {
    int MyValue();
}

a class

@MyAnnotation
public class MyClass {

}

will be a compile error without a value.