1. Generating no-args/default constructor using Lombok
Lombok @NoArgsConstructor will generate a no arguments/default constructor, 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 NoArgsDemo1.java
@NoArgsConstructor public class NoArgsDemo1 { private Long id; private String username; }
DeLomboked NoArgsDemo1.java
public class NoArgsDemo1 { private Long id; private String username; public NoArgsDemo1() { } }
2. @NoArgsConstructor vs static and final fields
You will get a compiler error if class has any non initialized final
fields. You need to set attribute value force=true
if you have any non initialized final
fields and all non initialized final
fields set to their default values within generated constructor. static
fields are not initialized within generated constructor for @NoArgsConstructor
.
Lomboked NoArgsDemo2.java
@NoArgsConstructor(force = true) public class NoArgsDemo2 { private final Long id; private final String username; private final double minSalary; private final int defaultRole = 1; private static boolean status; }
DeLomboked NoArgsDemo2.java
public class NoArgsDemo2 { private final Long id; private final String username; private final double minSalary; private final int defaultRole = 1; private static boolean status; public NoArgsDemo2() { this.id = null; this.username = null; this.minSalary = 0.0; } }
3. No-args constructor with non-null fields
Like @AllArgsConstructor
generates check for the non-null fields (fields declared as @NonNull), no check is generated for @NoArgsConstructor
Lomboked NoArgsDemo3.java
@NoArgsConstructor public class NoArgsDemo3 { private Long id; @NonNull private String username; }
DeLomboked NoArgsDemo3.java
public class NoArgsDemo3 { private Long id; @NonNull private String username; public NoArgsDemo3() { } }
4. Generating private no-args/default constructor
Lombok generates a public
no-args constructor by default for the @NoArgsConstructor
. To generate private
no-args constructor declare @NoArgsConstructor(access = AccessLevel.PRIVATE)
. access
attribute of @NoArgsConstructor
allows you to change the access modifier of the generated constructor.
Lomboked NoArgsDemo4.java
@NoArgsConstructor(access = AccessLevel.PRIVATE) public class NoArgsDemo4 { private Long id; private String username; }
DeLomboked NoArgsDemo4.java
public class NoArgsDemo4 { private Long id; private String username; private NoArgsDemo4() { } }
5. Creating a static factory method using @NoArgsConstructor
@NoArgsConstructor(staticName = "getInstance")
generates a static
factory method named with getInstance, staticName
attribute of @NoArgsConstructor
allows us to generates a private no-args constructors and an additional static
factory method that wraps around the private
constructor is generated.
Lomboked NoArgsDemo5.java
@NoArgsConstructor(staticName = "getInstance") public class NoArgsDemo5 { private Long id; private String username; }
DeLomboked NoArgsDemo5.java
public class NoArgsDemo5 { private Long id; private String username; private NoArgsDemo5() { } public static NoArgsDemo5 getInstance() { return new NoArgsDemo5(); } }
6. Put annotations on generated no-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:
@NoArgsConstructor([email protected]__({@AnnotationsGoHere}))
- From JDK8:
@NoArgsConstructor(onConstructor_={@AnnotationsGohere})
// note the underscore afteronConstructor
.
Lomboked NoArgsDemo6.java
@NoArgsConstructor(onConstructor_= @Deprecated ) public class NoArgsDemo6 { private Long id; private String username; }
DeLomboked NoArgsDemo6.java
public class NoArgsDemo6 { private Long id; private String username; @Deprecated public NoArgsDemo6() { } }
7. Conclusion
In this guide we have covered Lombok NoArgsConstructor 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