简体   繁体   English

lombok AllArgsConstructor 注释中是否包含 static 变量?

[英]Are static variables included in lombok AllArgsConstructor annotation?

The title pretty much sums it up.标题几乎概括了它。 I'm wondering if I need to include static variables (which I probably don't) in the constructor.我想知道是否需要在构造函数中包含 static 变量(我可能不需要)。

Given that static variables are static I probably dont.鉴于 static 变量是 static 我可能不知道。 But I didnt find any answers to this question on stackoverflow.但是我在stackoverflow上没有找到这个问题的任何答案。

No static fields are skipped while using these lombok annotations使用这些lombok注释时不会跳过任何 static 字段

@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor

Static fields are skipped by these annotations.这些注释跳过了 Static 字段。

If you want to declare constructor with static fields then you can declare the one explicitly, but you might end up with compiler error if any of those constructors have same signature如果要使用 static 字段声明构造函数,则可以显式声明该构造函数,但如果这些构造函数中的任何一个具有相同的签名,则最终可能会出现编译器错误

Unlike most other lombok annotations, the existence of an explicit constructor does not stop these annotations from generating their own constructor.与大多数其他 lombok 注释不同,显式构造函数的存在不会阻止这些注释生成自己的构造函数。 This means you can write your own specialized constructor, and let lombok generate the boilerplate ones as well.这意味着您可以编写自己的专用构造函数,并让 lombok 生成样板构造函数。 If a conflict arises (one of your constructors ends up with the same signature as one that lombok generates), a compiler error will occur.如果发生冲突(您的构造函数之一最终具有与 lombok 生成的签名相同的签名),则会发生编译器错误。

The answer is no as you can check in javadoc:答案是否定的,您可以查看 javadoc:

An all-args constructor requires one argument for every field in the class.全参数构造函数需要 class 中的每个字段都有一个参数。

or official documentation: https://projectlombok.org/features/Constructor或官方文档: https://projectlombok.org/features/Constructor

RequiredArgsConstructor includes in the constructor all final fields. RequiredArgsConstructor在构造函数中包含所有final字段。 However, you cannot have a static final variable not initialized (as they can be used even with no instance of that class)但是,您不能有未初始化的static final变量(因为即使没有该类的实例也可以使用它们)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM