[Java] How to use @Builder and @NoArgsConstructor together

less than 1 minute read

Task

An error occurs when @Builder and @NoArgsConstructor are used together as shown below.


@Builder
@NoArgsConstructor
public class MyName {
     private String first;
     private String last;
}

Resolution

You can add @AllArgsConstructor annotation or add a constructor with all fields.


@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MyName {
     private String first;
     private String last;
}

   .. or ..


@Builder
@NoArgsConstructor
public class MyName {
     private String first;
     private String last;

     MyName(String first, String last) {...}
}