[英]Is it legal to put annotation after access modifier in Java 7? Or Java 8?
This is usual code: 这是通常的代码:
@Autowire
private Service service;
But with Java 7 this also works (and shorter): 但是使用Java 7,这也可以(并且更短):
private @Autowire Service service;
Is that legal in Java 8 (have same semantic)? 这在Java 8中是否合法(具有相同的语义)? Is that bad coding practice?
这是不好的编码练习吗?
According to documentation 根据文件
In Java 7 : 在Java 7中:
Annotations can be applied to declarations: declarations of classes, fields, methods, and other program elements.
注释可以应用于声明:类,字段,方法和其他程序元素的声明。 When used on a declaration, each annotation often appears, by convention , on its own line.
当在声明中使用时,每个注释通常按照惯例出现在它自己的行上。
As of the Java SE 8 release, annotations can also be applied to the use of types. 从Java SE 8发行版开始,注释也可以应用于类型的使用。 :
:
Class instance creation expression: 类实例创建表达式:
new @Interned MyObject();
Type cast: 输入:
myString = (@NonNull String) str;
implements clause: 实现条款:
class UnmodifiableList<T> implements
@Readonly List<@Readonly T> { ... }
Thrown exception declaration: 抛出的异常声明:
void monitorTemperature() throws
@Critical TemperatureException { ... }
According to official Java 7 grammar this is legal: 根据官方Java 7语法,这是合法的:
Modifier:
Annotation
public
protected
private
static
abstract
final
native
synchronized
transient
volatile
strictfp
ClassOrInterfaceDeclaration:
{Modifier} (ClassDeclaration | InterfaceDeclaration)
...
Grammar for Java 8 also seems to allow free mixing of modifiers: Java 8的语法似乎也允许自由混合修饰符:
MethodDeclaration:
{MethodModifier} MethodHeader MethodBody
MethodModifier:
Annotation public protected private
abstract static final synchronized native strictfp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.