Search code examples
springspring-bootspring-data-jpalombokspring-annotations

Lombok: Annotation that generates only few paramteres in Constructor


I want to know if there's an annotation that generates a constructor which contains only, for example, two attributes of my choice. The annotation @AllArgsConstructor generates a constructor but with all parameters.

Edit: The solution was found but what if for example, I want a constructor for (String age, string D) and one for (String age, String F)?


Solution

  • Currently, there is no such feature and the only way to generate a constructor for the selected fields is by using @RequiredArgsConstructor and labeling only and only such fields as final.

    @RequiredArgsConstructor
    class Foo {
    
        private final String a;
        private final double b;
        private int c;
    }
    
    new Foo("String", 1.0);
    

    However, you don't want to design the class and its fields to satisfy the needs that Lombok doesn't provide.