Is the meaning of the "java keyword"?

In annotations, it has a special meaning - it allows you to skip parameter names when setting the annotation.

@Foo(bar = "abc") // a normal instantiation of an annotation @Foo("abc") // if bar were renamed 'value' 

Where is this documented? Is value keyword or not? See also .

+4
source share
3 answers

This is not a regular keyword, as it is not listed in section 3.9 of the JLS . In particular, you can use it as an identifier anywhere, as far as I know.

The use of the default value for the annotation value is specified in section 9.7 :

The third form of annotation, a singleton annotation, is a shorthand for use with singleton annotation types:

 SingleElementAnnotation: @ TypeName ( ElementValue ) 

This is short for regular annotation:

  @TypeName ( value = ElementValue ) 
+5
source

No, value not a keyword in Java. If only one parameter is given for the annotation, and the annotation has only one element called value , then when using the annotation, the name value can be omitted. The annotations are explained here: http://download.oracle.com/javase/1.5.0/docs/guide/language/annotations.html and documented in section 9.7 in JLS:

+6
source

value is the default field in which annotation data is placed. value however is not a java keyword (thanks for the reminder to @gustafc).

This is documented where you expect it to be - in the official documentation, which says:

In a single element annotation, the element must have a name

and

It is permissible to omit the element name and is equal to the sign (=) in the singleton annotation, whose element name matters

+3
source

All Articles