Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the order of parameters in @AllArgsConstructor in lombok

Tags:

java

lombok

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 ?

like image 543
prime Avatar asked Mar 05 '18 08:03

prime


People also ask

What is @AllArgsConstructor staticName of?

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.

What is @AllArgsConstructor annotation in spring boot?

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.

What is @RequiredArgsConstructor used for?

@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.

Does Lombok create constructor?

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).


1 Answers

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

like image 103
ntalbs Avatar answered Sep 21 '22 06:09

ntalbs