Lombok: How to specify a one arg constructor?

barbara picture barbara · Dec 18, 2014 · Viewed 44.9k times · Source

Using Lombok, is it possible to specify a one arg constructor?

My intention is to use Lombok annotations to create a constructor such as the one below.

class MyClass {
    private String param;
    private Integer count;

    public MyClass(String param) {
        this.param = param;
    }
}

Answer

maaartinus picture maaartinus · Dec 18, 2014

Lombok doesn't let you to specify the fields exactly, but there are 3 annotations to choose from. With

@RequiredArgsConstructor class MyClass {
    private final String param;
    private Integer count;
}

you can get it. An argument is required if it's not initialized inline and final or @NonNull.