英文:
How does @value in lombok work in this example
问题
以下是翻译好的内容:
这是我的简单类
@Value
public class Task {
private static int numberOfTasks = 0;
private int id = ++numberOfTasks;
private String name;
private Timestamp startTime;
private Timestamp endTime;
}
阅读@Value的文档,上面写着:
在实践中,@Value相当于:final @ToString @EqualsAndHashCode @AllArgsConstructor @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @Getter,
这意味着有一个AllArgsConstructor。然而,如果我尝试将字段设为final,编译器会报错说字段未初始化。
(尽管Lombok在@Value的情况下声称会将字段设为final)
有人可以帮忙解决一下吗?
这就是我的意思:
英文:
This is my simple class
@Value
public class Task {
private static int numberOfTasks = 0;
private int id = ++numberOfTasks;
private String name;
private Timestamp startTime;
private Timestamp endTime;
}
Reading the Documentation of @Value, It says:
In practice, @Value is shorthand for: final @ToString @EqualsAndHashCode @AllArgsConstructor @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @Getter,
So that means there is a AllArguConstructor. However, If I try to make the field as final, I get a compiler error that the filed is not initialised.
(though also Lombok claims to make the field as final in the case of @Value)
Could anyone help here?
This is what I mean:
专注分享java语言的经验与见解,让所有开发者获益!
评论