Java
Lombok
AllArgsConstructor
Java Annotations
Code Generation

Lombok: exclude one field from the @AllArgsConstructor constructor

Codemia Knowledge Hub ·

@AllArgsConstructor has no option to exclude a field. It generates a constructor for every non-static field by design. To leave one out, make the fields you want final or @NonNull and use @RequiredArgsConstructor, switch to @Builder, or write the constructor yourself.

What @AllArgsConstructor generates

It includes every instance field except static ones. This class:

java
1import lombok.AllArgsConstructor;
2
3@AllArgsConstructor
4public class User {
5    private String name;
6    private int age;
7    private String email;
8}

gets a constructor equivalent to:

java
1public User(String name, int age, String email) {
2    this.name = name;
3    this.age = age;
4    this.email = email;
5}

If you need a constructor without email, @AllArgsConstructor is the wrong annotation.

Use @RequiredArgsConstructor for the fields you need

Mark the constructor fields as final or @NonNull and use @RequiredArgsConstructor:

java
1import lombok.NonNull;
2import lombok.RequiredArgsConstructor;
3
4@RequiredArgsConstructor
5public class User {
6    @NonNull
7    private final String name;
8
9    private final int age;
10
11    private String email;
12}

Lombok generates a constructor for the required fields only. email is not required, so it is left out. When some fields are required and others optional, this is usually the cleanest Lombok-native solution.

Write the constructor yourself

If the inclusion rule is more specific than Lombok's constructor annotations can express, write it by hand:

java
1public class User {
2    private final String name;
3    private final int age;
4    private String email;
5
6    public User(String name, int age) {
7        this.name = name;
8        this.age = age;
9    }
10}

This is often better than fighting the annotation model. Lombok helps when it makes code clearer, not when it forces awkward workarounds.

Use @Builder when many fields are optional

With many optional fields, a builder usually reads better than any constructor:

java
1import lombok.Builder;
2
3@Builder
4public class User {
5    private String name;
6    private int age;
7    private String email;
8}

Callers set only what they need:

java
1User user = User.builder()
2    .name("Ada")
3    .age(34)
4    .build();

This works well when leaving fields out is normal rather than an exception.

What does not work

@ConstructorProperties and similar annotations do not change what Lombok includes in @AllArgsConstructor. They may affect metadata or constructors you write yourself, but they do not make @AllArgsConstructor selective.

Other traps:

  • Adding annotations that affect reflection metadata but not Lombok's constructor generation.
  • Using @AllArgsConstructor when the real model is "some fields required, some optional."
  • Forcing a Lombok solution when a handwritten constructor would be clearer.
  • Forgetting that static fields are already excluded, which can confuse you when testing examples.

The general rule: pick the annotation that matches the object model you want instead of bending a convenience annotation into a custom code generator.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms