Is it legal to put annotation after access modifier in Java 7? Or Java 8?(在 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 { ... }
这篇关于在 Java 7 中的访问修饰符之后放置注释是否合法?还是 Java 8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 7 中的访问修饰符之后放置注释是否合法?还是 Java 8?
基础教程推荐
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
