The Special value Element
What you'll learn: How using the conventional name value for an annotation element lets you skip the parameter name in single-element annotations.
When you define a custom annotation, Java provides syntactic sugar for a cleaner usage pattern. If your annotation has only one element (or if only one element lacks a default value), and you name it value, callers can omit the element name entirely.
How It Works
Normally, when applying an annotation, you must explicitly name each element:
@MyAnnotation(message = "Hello")
But if you name your element value, Java allows this shorthand:
@MyAnnotation("Hello")
Both syntaxes work, but the shorter form improves readability when your annotation represents a single, obvious piece of information.
Defining an Annotation with value
public @interface MyAnnotation {
String value();
}
Now you can use it without naming the parameter:
@MyAnnotation("This is concise!")
public class Example { }
If value has a default, you can still omit it entirely:
public @interface Path {
String value() default "/";
}
@Path // Uses default "/"
public class Root { }
Think of It Like This
The value element is like a primary key—the most important piece of data your annotation carries. Just as you'd say "the path is /api" rather than "the value named 'path' is /api," the shorthand reflects natural language.
This convention is used throughout Java's standard annotations when a single piece of data dominates the annotation's purpose.
Key Takeaway: Naming an annotation element value enables shorthand syntax where you can omit the element name, making single-element annotations cleaner and more readable.