Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do annotations apply to all variables in a declaration statement?

If multiple fields are declared in a single statement using a field annotation, does the annotation apply to all of the fields?

For example, will the following result in x, y, and z all having the @Nullable annotation?

@Nullable public Integer x, y, z;

I'm looking for an official specification on this, but have had trouble finding one.

like image 601
benjer3 Avatar asked Jul 26 '18 00:07

benjer3


People also ask

When to apply annotations in Java?

These annotations can be applied just before the declaration of an element (constructor, method, classes, etc). Do keep these certain points as rules for custom annotations before implementing user-defined annotations. AnnotationName is an identifier.

What is the Declaration and initialization of variables?

These variables consist of a data type, the variable name, and the value to be assigned to the variable. Unless and until the variables are declared and initialized, they cannot be used in the program. Let us learn more about the Declaration and Initialization of Variables in this article below. 1 What is Declaration and Initialization?

Do annotations change the action of a compiled program?

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. Annotations are not pure comments as they can change the way a program is treated by the compiler.

How do you mark a method as annotated in Java?

Any declaration can be marked with annotation by placing it above that declaration. As of Java 8, annotations can also be placed before a type. 1. Above declarations As mentioned above, Java annotations can be placed above class, method, interface, field, and other program element declarations.


2 Answers

I think the closest we can get is this, starting from §8.3:

FieldDeclaration:
  {FieldModifier} UnannType VariableDeclaratorList ;

VariableDeclaratorList:
  VariableDeclarator {, VariableDeclarator}

VariableDeclarator:
  VariableDeclaratorId [= VariableInitializer]

And FieldModifier is:

FieldModifier:
  (one of)
  Annotation public protected private
  static final transient volatile 

(So an annotation is a field modifier.)

And back to §8.3:

More than one field may be declared in a single FieldDeclaration by using more than one declarator; the FieldModifiers and UnannType apply to all the declarators in the declaration.

So in otherwords, given:

@Nullable public Integer x, y, z;

We know that the field modifiers @Nullable public apply to all of x, y and z.

Also, reading a bit more of §8.3 as well as §9.74 will clarify that that's the case even if @Nullable is declared as a type annotation (i.e. @Target(ElementType.TYPE_USE)):

It is possible for an annotation to appear at a syntactic location in a program where it could plausibly apply to a declaration, or a type, or both. This can happen in any of the five declaration contexts where modifiers immediately precede the type of the declared entity:

  • […]

  • Field declarations (including enum constants)

  • […]

The grammar of the Java programming language unambiguously treats annotations at these locations as modifiers for a declaration (§8.3), […].

In other words, an annotation in such a location is always treated syntactically as a field modifier, even if it's not considered a declaration annotation (because it's not declared with @Target(ElementType.FIELD)). This is also hinted at in the grammar for field declarations, where the type is given as UnannType, i.e. unannotated type.

like image 114
Radiodef Avatar answered Sep 19 '22 03:09

Radiodef


Yes, they all have the annotation.

The annotation is modifier of the type Integer, not for the x. So all the x, y, z are type @Nullable Integer

like image 40
Dean Xu Avatar answered Sep 22 '22 03:09

Dean Xu