1. Generating All-args constructor using Lombok
Lombok @AllArgsConstructor generates a constructor with one parameter for each field in your class, by default generated constructor will be public
.
Make sure you already installed Lombok setup for your IDE. To Setup in Eclipse or in Spring Tool Suite refer to our Lombok Maven example setup with Eclipse.
Lomboked AllArgsDemo1.java
@AllArgsConstructor public class AllArgsDemo1 { private Long id; private String username; }
DeLomboked AllArgsDemo1.java
public class AllArgsDemo1 { private Long id; private String username; public AllArgsDemo1(final Long id, final String username) { this.id = id; this.username = username; } }
2. All-args constructor with non-null fields
The fields marked with @NonNull
in your class result in null check on those parameter within generated all-args constructor.
Lomboked AllArgsDemo2.java
@AllArgsConstructor public class AllArgsDemo2 { private Long id; @NonNull private String username; }
DeLomboked AllArgsDemo2.java
public class AllArgsDemo2 { private Long id; @NonNull private String username; public AllArgsDemo2(final Long id, @NonNull final String username) { if (username == null) { throw new NullPointerException("username is marked non-null but is null"); } this.id = id; this.username = username; } }
3. @AllArgsConstructor vs static and final fields
- Lombok never generates constructor argument for the
static
fields for@AllArgsConstructor
. - For
@AllArgsConstructor
Lombok never generates constructor argument for thefinal
fields if they are initialized with value, otherwise an argument will be generated.
Lomboked AllArgsDemo3.java
@AllArgsConstructor public class AllArgsDemo3 { private Long id; private static boolean defaultStatus; private final double minSalary = 10000.00; private final int defaultRole; }
DeLomboked AllArgsDemo3.java
public class AllArgsDemo3 { private Long id; private static boolean defaultStatus; private final double minSalary = 10000.0; private final int defaultRole; public AllArgsDemo3(final Long id, final int defaultRole) { this.id = id; this.defaultRole = defaultRole; } }
4. Generating private all-args constructor
Lombok generates a public all-args constructor by default for the @AllArgsConstructor
. To generate private
all-args constructor define @AllArgsConstructor(access = AccessLevel.PRIVATE)
. access
attribute of @AllArgsConstructor
allows you to change the access modifier of the generated constructor.
Lomboked AllArgsDemo4.java
@AllArgsConstructor(access = AccessLevel.PRIVATE) public class AllArgsDemo4 { private Long id; private String username; }
DeLomboked AllArgsDemo4.java
public class AllArgsDemo4 { private Long id; private String username; private AllArgsDemo4(final Long id, final String username) { this.id = id; this.username = username; } }
5. Creating a static factory method using @AllArgsConstructor
If we would like to create instance using a static
factory method, staticName
attribute of @AllArgsConstructor
allows us to generates a private all-args constructors and an additional static
factory method that wraps around the private
constructor is generated. Let’s have a look into following example.
Lomboked AllArgsDemo5.java
@AllArgsConstructor(staticName = "getInstance") public class AllArgsDemo5 { private Long id; private String username; }
DeLomboked AllArgsDemo5.java
public class AllArgsDemo5 { private Long id; private String username; private AllArgsDemo5(final Long id, final String username) { this.id = id; this.username = username; } public static AllArgsDemo5 getInstance(final Long id, final String username) { return new AllArgsDemo5(id, username); } }
6. Put annotations on generated all-args constructor
Sometimes you may want to define annotations on top of constructor, for example when you are working with Spring framework or some other third part java libraries, you may need to declare annotations on top of constructor . onConstructor
attribute of @AllArgsConstructor
allows us to put annotations on generated all-args constructor.
- Up to JDK7:
@AllArgsConstructor([email protected]__({@AnnotationsGoHere}))
- From JDK8:
@AllArgsConstructor(onConstructor_={@AnnotationsGohere})
// note the underscore afteronConstructor
.
Lomboked AllArgsDemo6.java
@AllArgsConstructor( onConstructor_= @ConstructorProperties({"id", "username"})) public class AllArgsDemo6 { private Long id; private String username; }
DeLomboked AllArgsDemo6.java
public class AllArgsDemo6 { private Long id; private String username; @ConstructorProperties({"id", "username"}) public AllArgsDemo6(final Long id, final String username) { this.id = id; this.username = username; } }
7. Conclusion
In this guide we have covered Lombok AllArgsConstructor annotation examples with several options. You can refer Delombok Maven example to see how looks like lombok generated code for your Lomboked classes.
You can checkout source code at github.
You might be interested in our other following Lombok Tutorials :
- Lombok @Getter @Setter and lazy getters examples
- Lombok @Data
- Lombok @Value
- Lombok @Builder
- Lombok @NonNull
- Lombok @Slf4j
- Lombok @Singular
- Lombok Spring Boot Example