Java
Lombok
AllArgsConstructor
Java Annotations
Code Generation

Java Lombok Omitting one field in AllArgsConstructor?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

If you want Lombok to generate a constructor but skip one specific field, @AllArgsConstructor is usually the wrong annotation. Its job is exactly what the name says: generate a constructor for all non-static fields, so there is no built-in "exclude this one field" switch for arbitrary omission.

What @AllArgsConstructor Really Does

@AllArgsConstructor includes every instance field except static fields. That means code like this:

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

generates 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 your requirement is "generate a constructor but leave out email," then @AllArgsConstructor is not the right match.

The Usual Fix: Use @RequiredArgsConstructor

If only some fields should be in the constructor, mark those 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}

Now Lombok generates a constructor only for the required fields, and email is left out because it is not required.

This is usually the cleanest Lombok-native solution when some fields are constructor-required and others are optional.

Another Fix: Write the Constructor Yourself

If the inclusion rule is more specific than Lombok's standard constructor annotations can express, just write the constructor manually.

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 is useful when it makes the code clearer, not when it forces awkward workarounds.

When @Builder Is a Better Fit

If the class has many optional fields, a builder is often more readable than any constructor variant.

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

Then callers can choose what to set:

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

That is especially useful when omitting fields is normal rather than exceptional.

What Does Not Solve It

Annotations such as @ConstructorProperties do not change what Lombok decides to include in @AllArgsConstructor. They may affect metadata or explicit constructors you write yourself, but they do not turn @AllArgsConstructor into a selective constructor generator.

So the answer is not "tell @AllArgsConstructor to skip one field." The answer is usually:

  • use @RequiredArgsConstructor
  • or use @Builder
  • or write the constructor manually

That is a good example of a broader Lombok rule: choose the annotation that matches the object model you actually want, instead of trying to bend a convenience annotation into a custom code generator.

Common Pitfalls

  • Expecting @AllArgsConstructor to support arbitrary field exclusion.
  • Adding annotations that affect reflection metadata but not Lombok 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 cause confusion when testing examples.

Summary

  • '@AllArgsConstructor includes all non-static instance fields by design.'
  • It does not provide a general "omit this one field" option.
  • Use @RequiredArgsConstructor when only required fields belong in the constructor.
  • Use @Builder when optional fields are common.
  • When Lombok stops matching the design cleanly, write the constructor yourself.

Related reading
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
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.