
Understand the Difference Between @RequiredArgsConstructor, @AllArgsConstructor, and @NoArgsConstructor in Lombok
When working with Java, one of the most common tasks developers face is creating constructors for their classes. In a traditional Java development environment, you might find yourself writing a lot of boilerplate code just to create constructors....
1. Understanding Lombok Annotations for Constructors
Lombok simplifies the process of creating constructors in Java classes by automatically generating them at compile time. Let's break down each of these annotations to understand their specific roles and how they differ from each other.
1.1 What is @NoArgsConstructor?

The @NoArgsConstructor annotation generates a no-argument constructor. This is particularly useful when you need to create an instance of a class without passing any parameters.
In the above code, Lombok will generate a no-argument constructor for the Example class, which is equivalent to:
public Example() {}
1.2 What is @AllArgsConstructor?
The @AllArgsConstructor annotation generates a constructor that takes one parameter for each field in the class. This is particularly useful when you want to initialize all fields of a class in a single constructor.
Example:
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class Person {
private String name;
private int age;
}
Here, Lombok will generate an all-arguments constructor for the Person class, which would look like:
public Person(String name, int age) {
this.name = name;
this.age = age;
}
1.3 What is @RequiredArgsConstructor?
The @RequiredArgsConstructor annotation generates a constructor for all final fields, as well as fields that are marked as @NonNull. This is helpful when you want to ensure that certain fields are always initialized when creating an instance of a class.
Example:
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class Person {
private final String name;
private int age;
}
For the above class, Lombok will generate a constructor that takes only the name parameter because it is marked as final:
public Person(String name) {
this.name = name;
}
2. Key Differences and Use Cases
Understanding the key differences between these annotations is crucial for selecting the right one for your needs. Each annotation has its own specific use cases and is best suited for different scenarios.
2.1 When to Use @NoArgsConstructor?
Use @NoArgsConstructor when you need to create an instance of a class without passing any parameters. This is especially useful in frameworks like Hibernate, where a no-argument constructor is required for entity classes.
Use Case Example:
@NoArgsConstructor
@Entity
public class User {
@Id
private Long id;
private String username;
}
In this case, @NoArgsConstructor is necessary because Hibernate uses a proxy mechanism that requires a no-argument constructor.
2.2 When to Use @AllArgsConstructor?
Use @AllArgsConstructor when you need to create an instance of a class and initialize all of its fields. This is useful when you have a class with multiple fields that should be initialized in one go.
Use Case Example:
@AllArgsConstructor
public class Order {
private String orderId;
private String product;
private int quantity;
}
Here, @AllArgsConstructor allows you to create an Order object with all fields initialized:
Order order = new Order("123", "Laptop", 2);
2.3 When to Use @RequiredArgsConstructor?
Use @RequiredArgsConstructor when you want to ensure that certain fields, especially final or @NonNull fields, are initialized upon object creation. This annotation is useful in service classes where certain dependencies must be injected at the time of object creation.
Use Case Example:
@RequiredArgsConstructor
public class CustomerService {
private final CustomerRepository repository;
private final NotificationService notificationService;
}
This ensures that the repository and notificationService are always initialized when creating an instance of CustomerService:
CustomerService service = new CustomerService(new CustomerRepository(), new NotificationService());
3. Conclusion
Understanding the differences between @RequiredArgsConstructor, @AllArgsConstructor, and @NoArgsConstructor is essential for effective Java development. By knowing when and why to use each annotation, you can write cleaner, more maintainable code that reduces boilerplate and improves readability. Remember, choosing the right constructor annotation depends on your specific requirements, such as object initialization, immutability, and framework constraints.
If you have any questions or need further clarification on these Lombok annotations, feel free to comment below. Happy coding!
Read more at : Understand the Difference Between @RequiredArgsConstructor, @AllArgsConstructor, and @NoArgsConstructor in Lombok