189

If I specify @AllArgsConstructor using Lombok, it will generate a constructor for setting all the declared (not final, not static) fields. Is it possible to omit some field and this leave generated constructor for all other fields?

2
  • 2
    It wouldn't really be an all-args ctor then. Commented May 20, 2014 at 13:36
  • 4
    Sure thing. Maybe there is some solution with lombok? Commented May 20, 2014 at 13:42

7 Answers 7

232

No that is not possible. There is a feature request to create a @SomeArgsConstructor where you can specify a list of involved fields.

Full disclosure: I am one of the core Project Lombok developers.

Sign up to request clarification or add additional context in comments.

8 Comments

@SomeArgsConstructor missed in lombok 1.16.20
Is @SomeArgsConstructor available in 2020?
I hope we will see such a constructor in the upcoming 2021 (it looks like the feature request was rejected for reasons I don't understand)
See answer(s) about @RequiredArgsConstructor, as that annotation may meet most of the use-cases for OP's question.
As you have with @EqualsAndHashCode.Exclude you could add @AllArgsConstructor.Exclude in front of the field.
|
192

Alternatively, you could use @RequiredArgsConstructor. This adds a constructor for all fields that are either @NonNull or final.

See the documentation

2 Comments

This is a nice workaround, using @NonNull. But be aware that this does not work with fields having default-values.
This worked well for me, and I even marked the omitted field with @Transient to avoid it being tracked by java persistence layer since it was in my DAO.
39

Just in case it helps, initialized final fields are excluded.

@AllArgsConstructor
class SomeClass {
    final String s;
    final int i;
    final List<String> list = new ArrayList<>(); // excluded in constructor
}

var x = new SomeClass("hello", 1);

It makes sense especially for collections, or other mutable classes.

This solution can be used together with the other solution here, about using @RequiredArgsConstructor:

@RequiredArgsConstructor
class SomeClass2 {
    final String s;
    int i; // excluded because it's not final
    final List<String> list = new ArrayList<>(); // excluded because it's initialized
}

var x = new SomeClass2("hello");

6 Comments

Important addition: "initialized final fields are excluded" -> If the field is only initialized but not final the constructor (generated by AllArgsConstructor) will be generated with this field as well :)
Lombok is intended to generate code for common cases, not for specific scenarios. You should code your particular constructors explicitly.
Your answer helped a ton: in case the fields you want to exclude are final because they are constants.
I used this option and it works.
To me this answer is underrated and should be first looked by anyone searching answer on how to skip a field in lombok constructor.
|
19

A good way to go around it in some cases would be to use the @Builder

3 Comments

Lets say "a way" but not "a good way". If you want an AllArgsConstructor to guarantee that the user provides all (required) members a std builder is not the way to do it.
I said in some cases, and I still stand by it
15

This can be done using two annotations from Lombok:

Please find the example as follows:

package com.ss.model;

import lombok.*;

@Getter
@Setter
@RequiredArgsConstructor
@ToString
public class Employee {

    private int id;

    @NonNull
    private String firstName;

    @NonNull
    private String lastName;

    @NonNull
    private int age;

    @NonNull
    private String address;
}

And then you can create an object as shown below:

Employee employee = new Employee("FirstName", "LastName", 27, "Address");

2 Comments

Hi I wanna ask if there's any idea how to generate two type of constructors, for example one contains Age & Lastname, one cantains Address & Age
Hi @CompteGmail, it is better to define the constructors individually
3

When you needs a constructor with just some attributes, you can use

@RequiredArgsConstructor at class level and declare the choosed attributes as final. Then, if you need an empty constructor, you can use something like

@NoArgsConstructor(access=AccessLevel.PRIVATE, force=true)

This annotation will create an empty JPA's constructor and the attributes will be initialized with default values (0 for int, null for String and so on).

Example:

@Data
@Entity
@Table(name = "VetFiles")
@RequiredArgsConstructor
@NoArgsConstructor(access=AccessLevel.PRIVATE, force=true)
public class FileInfo implements Serializable {

    private static final long serialVersionUID = 6719621520531075147L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private final String name;
    private final String url;

    @ManyToOne
    private Thing thing;

Comments

-1

Lombok is meant to take care of the boilerplate code for your POJOs. Customized constructors/setters/getters/toString/copy etc are not on the boilerplate side of code. For these cases, every Java IDE provide easy to use code generators to help you do things in no time. In your case a

public MyClass(String firstName, String lastName) {....}

is much more readable and makes more sense than a hypothetic:

@AllArgsConstructor(exclude = "id", exclude = "phone")

Have fun!

1 Comment

IDE generated boilerplate code infests the source code. And whenever you make a change in your class, you must remember to regenerate it again. @AllArgsConstructor doesn't suffer from this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.