Home Lombok Lombok @RequiredArgsConstructor examples

Lombok @RequiredArgsConstructor examples

1. Lombok @RequiredArgsConstructor

Generates a constructor with required arguments. Required arguments are uninitialized final fields and fields with constraints such as @NonNull. Default access modifier is 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 RequiredArgsDemo1.java

@RequiredArgsConstructor
public class RequiredArgsDemo1 {

	private Long id;

	@NonNull
	private String username;

	@NonNull
	private String email;

	private final boolean status;
}

Delomboked RequiredArgsDemo1.java

public class RequiredArgsDemo1 {
	private Long id;
	@NonNull
	private String username;
	@NonNull
	private String email;
	private final boolean status;

	public RequiredArgsDemo1(
			@NonNull final String username, 
			@NonNull final String email, 
			final boolean status
			) {
		
		if (username == null) {
			throw new NullPointerException("username is marked non-null but is null");
		}
		if (email == null) {
			throw new NullPointerException("email is marked non-null but is null");
		}
		this.username = username;
		this.email = email;
		this.status = status;
	}
}

2. Lombok @RequiredArgsConstructor vs non-final and static fields

Lombok @RequiredArgsConstructor will not generate any argument for following fields

  1. Non-final fields.
  2. Initialized final fields.
  3. static fields.
  4. Initialized non-null fields.

Lomboked RequiredArgsDemo2.java

@RequiredArgsConstructor
public class RequiredArgsDemo2 {

	private Long id;

	@NonNull
	private String username = "anonymous";

	private final int defaultRole = 1;
	
	private static double minSalary = 10000.00;
}

Delomboked RequiredArgsDemo2.java

public class RequiredArgsDemo2 {
	private Long id;
	@NonNull
	private String username = "anonymous";
	private final int defaultRole = 1;
	private static double minSalary = 10000.0;

	public RequiredArgsDemo2() {
	}
}

3. Generating private constructor using @RequiredArgsConstructor

Lombok generates a public constructor by default for the @RequiredArgsConstructor. To generate private constructor declare @RequiredArgsConstructor(access = AccessLevel.PRIVATE). access attribute of @RequiredArgsConstructor allows you to change the access modifier of the generated constructor.

Lomboked RequiredArgsDemo3.java

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class RequiredArgsDemo3 {

	private Long id;

	@NonNull
	private String username;
	
}

Delomboked RequiredArgsDemo3.java

public class RequiredArgsDemo3 {
	private Long id;
	@NonNull
	private String username;

	private RequiredArgsDemo3(@NonNull final String username) {
		if (username == null) {
			throw new NullPointerException("username is marked non-null but is null");
		}
		this.username = username;
	}
}

4. Creating a static factory method using @RequiredArgsConstructor

If we would like to create instance using a static factory method, staticName attribute of @RequiredArgsConstructor allows us to generates a private constructor with one argument for each uninitialized final, non-null fields and an additional static factory method that wraps around the private constructor is generated. Let’s have a look into following example.

Lomboked RequiredArgsDemo4.java

@RequiredArgsConstructor(staticName = "getInstance")
public class RequiredArgsDemo4 {

	private Long id;

	@NonNull
	private String username;

	@NonNull
	private String email;

	private final boolean status;
	
}

Delomboked RequiredArgsDemo4.java

public class RequiredArgsDemo4 {
	private Long id;
	@NonNull
	private String username;
	@NonNull
	private String email;
	private final boolean status;

	private RequiredArgsDemo4(
			@NonNull final String username,
			@NonNull final String email,
			final boolean status) {
		
		if (username == null) {
			throw new NullPointerException("username is marked non-null but is null");
		}
		if (email == null) {
			throw new NullPointerException("email is marked non-null but is null");
		}
		this.username = username;
		this.email = email;
		this.status = status;
	}

	public static RequiredArgsDemo4 getInstance(
			@NonNull final String username, 
			@NonNull final String email, 
			final boolean status) {
		
		return new RequiredArgsDemo4(username, email, status);
	}
}

5. Put annotations on constructor generated by @RequiredArgsConstructor

Sometimes you may want to define annotations on top of constructor, for example when you are working with Spring framework or some other third party java libraries, you may need to declare annotations on top of constructor. onConstructor attribute of @RequiredArgsConstructor allows us to put annotations on generated required-args constructor.

  1. Up to JDK7: @NoArgsConstructor([email protected]__({@AnnotationsGoHere}))
  2. From JDK8: @NoArgsConstructor(onConstructor_={@AnnotationsGohere}) // note the underscore after onConstructor.

Lomboked RequiredArgsDemo5.java

@RequiredArgsConstructor(
		onConstructor_=
		@ConstructorProperties({"username"}))
public class RequiredArgsDemo5 {

	private Long id;

	@NonNull
	private String username;

}

Delomboked RequiredArgsDemo5.java

public class RequiredArgsDemo5 {
	private Long id;
	@NonNull
	private String username;

	@ConstructorProperties({"username"})
	public RequiredArgsDemo5(@NonNull final String username) {
		if (username == null) {
			throw new NullPointerException("username is marked non-null but is null");
		}
		this.username = username;
	}
}

6. Conclusion

In this guide we have covered Lombok RequiredArgsConstructor annotation examples with different 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 :

  1. Lombok @Getter @Setter and lazy getters examples
  2. Lombok @Data
  3. Lombok @Value
  4. Lombok @Builder
  5. Lombok Builder default values Examples
  6. Lombok @NonNull
  7. Lombok Spring Boot example
  8. Lombok @Slf4j
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories