Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are static variables included in lombok AllArgsConstructor annotation?

Tags:

java

lombok

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.

Given that static variables are static I suspect that they are probably not. But I didnt find any answers to this question on stackoverflow.

like image 219
hreinn Avatar asked Sep 03 '25 10:09

hreinn


2 Answers

No static fields are skipped while using these lombok annotations

@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor

Static fields are skipped by these annotations.

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

Unlike most other lombok annotations, the existence of an explicit constructor does not stop these annotations from generating their own constructor. This means you can write your own specialized constructor, and let lombok generate the boilerplate ones as well. If a conflict arises (one of your constructors ends up with the same signature as one that lombok generates), a compiler error will occur.

like image 123
Deadpool Avatar answered Sep 04 '25 23:09

Deadpool


The answer is no as you can check in javadoc:

An all-args constructor requires one argument for every field in the class.

or official documentation: https://projectlombok.org/features/Constructor

RequiredArgsConstructor includes in the constructor all final fields. However, you cannot have a static final variable not initialized (as they can be used even with no instance of that class)

like image 41
pedrohreis Avatar answered Sep 04 '25 23:09

pedrohreis