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:
gets a constructor equivalent to:
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:
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:
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:
Callers set only what they need:
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
@AllArgsConstructorwhen 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.
Related reading
- Java machine learning library for commercial use?
- Java memory model - volatile and x86
- Java merge 2 collections in O1
- Java method with return type compiles without return statement
- Java multi-threading Safe Publication
- Java Multiple class declarations in one file
- Java Multiple Inheritance
- Java Multithreading for IVRS with GSM Modem rxtx playing voice file making event listener stop working
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.