在 Java 7 中的访问修饰符之后放置注释是否合法?还是 Java 8?
这是通常的代码:
@Autowire
private Service service;
但是对于 Java 7,这也可以(并且更短):
But with Java 7 this also works (and shorter):
private @Autowire Service service;
这在 Java 8 中合法吗(具有相同的语义)?这是不好的编码习惯吗?
Is that legal in Java 8 (have same semantic)? Is that bad coding practice?
推荐答案
根据文档
在 Java 7 中:
In 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.
从 Java SE 8 版本开始,注解也可以应用于类型的使用.:
As of the Java SE 8 release, annotations can also be applied to the use of types. :
类实例创建表达式:
new @Interned MyObject();
类型转换:
myString = (@NonNull String) str;
实现子句:
class UnmodifiableList<T> implements
@Readonly List<@Readonly T> { ... }
抛出异常声明:
void monitorTemperature() throws
@Critical TemperatureException { ... }
相关文章