If I have a class like below,
import lombok.AllArgsConstructor;
@AllArgsConstructor
class MyClass{
private String one;
private Integer three;
private Integer two;
}
What will be the order of parameters in the generated constructor ? Is it always like below,
public MyClass(String one, Integer three, Integer two) {
this.one = one;
this.three = three;
this.two = two;
}
I noticed it's the order of declaration in the class itself. But need to confirm it. Couldn't find any documentation that verify that fact.
If not can we define the order of params in anyway ?
Creating a static factory method using @AllArgsConstructor If we would like to create instance using a static factory method, staticName attribute of @AllArgsConstructor allows us to generates a private all-args constructors and an additional static factory method that wraps around the private constructor is generated.
The @AllArgsConstructor annotation generates a constructor with one parameter for every field in the class. Fields that are annotated with @NonNull result in null checks with the corresponding parameters in the constructor. The annotation won't generate a parameter for the static and initialized final fields.
@RequiredArgsConstructor generates a constructor with 1 parameter for each field that requires special handling. All non-initialized final fields get a parameter, as well as any fields that are marked as @NonNull that aren't initialized where they are declared.
With Lombok, it's possible to generate a constructor for either all class's fields (with @AllArgsConstructor) or all final class's fields (with @RequiredArgsConstructor).
The lombok document on Constructor, it says: (the last sentence of the third paragraph. Or you can find 'sort' with your browser's find feature)
The order of the parameters match the order in which the fields appear in your class.
Though the sentence is in the paragraph for @RequiredArgsConstructor
, the same rule looks to apply to @AllArgsConstructor
, too.
https://projectlombok.org/features/constructor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With