Lombok • How to Use Constructor
Overview
To use the constructor with Lombok, you can annotate your class with @AllArgsConstructor or @RequiredArgsConstructor. @AllArgsConstructor generates a constructor that takes in all the fields in the class as arguments, while @RequiredArgsConstructor generates a constructor that takes in only the final or @NonNull fields as arguments.
@AllArgsConstructor
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class Person {
private String name;
private int age;
}
In the above example, Lombok will generate a constructor with two arguments, one for name and one for age.
Here is the equivalent de-Lombok (generated) code
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
You can then create an instance of Person using this constructor like this:
Person person = new Person("John", 30);
Lombok will handle the creation of the constructor for you, saving you from having to write the boilerplate code yourself.
@RequiredArgsConstructor
The @RequiredArgsConstructor is a Lombok annotation that generates a constructor with parameters for all final fields and/or fields annotated with @NonNull in a class.
Here’s an example:
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public class Car {
@NonNull private final String make;
@NonNull private final String model;
private int year;
}
Here is the equivalent de-Lombok (generated) code
public class Car {
private final String make;
private final String model;
private int year;
public Car(final String make, final String model) {
if (make == null) {
throw new NullPointerException("make");
}
if (model == null) {
throw new NullPointerException("model");
}
this.make = make;
this.model = model;
}
public String getMake() {
return this.make;
}
public String getModel() {
return this.model;
}
public int getYear() {
return this.year;
}
public void setYear(final int year) {
this.year = year;
}
}
As you can see, the Lombok annotations @Getter, @NonNull, and @RequiredArgsConstructor have been replaced by the constructor and explicit getter and setter methods.
In the constructor, Lombok’s @NonNull annotation has been replaced by explicit null checks, as well as an exception thrown when null values are passed to the constructor.
In the above example, Lombok will generate a constructor that takes in two arguments, make and model, which are both marked as final and @NonNull. The year field is not included in the constructor, as it is not marked as final or @NonNull.
You can then create an instance of Car using this constructor like this:
Car car = new Car("Toyota", "Camry");
Lombok will handle the creation of the constructor for you, saving you from having to write the boilerplate code yourself.
Note that if all the fields in a class are marked as final or @NonNull, @RequiredArgsConstructor will generate a constructor with no arguments.
In Conclusion
In conclusion, Lombok provides convenient annotations such as @AllArgsConstructor and @RequiredArgsConstructor that generate constructors for your classes. With @AllArgsConstructor, you can quickly create a constructor that takes in all the fields in the class as arguments, while @RequiredArgsConstructor generates a constructor that takes in only the final or @NonNull fields as arguments. Lombok handles the creation of the constructor, saving you from having to write the boilerplate code yourself. By using Lombok annotations, you can reduce the amount of code you need to write, making your code cleaner and more concise.
Lombok • The Good, The Bad, and The Ugly
Post Date: 07 Nov 2023
Within the Java development community, Lombok often emerges as a polarizing subject. This library’s chief aim is to minimize the tedium of boilerplate code—a persistent thorn in the side of many Java developers. Nevertheless, every tool brings its own concessions to the table.
Lombok • @Value Annotation Best Practices
Post Date: 26 Oct 2023
When it comes to clean coding and enhanced testability in Java applications, Project Lombok is a game-changer. Its @Value annotation not only simplifies your code but also enhances its readability, maintainability, and testability.
Lombok • @Data Annotation Best Practices
Post Date: 26 Oct 2023
When it comes to clean and efficient Java coding, the power of Project Lombok cannot be overstated. Specifically, the @Data annotation provided by Lombok stands out as a valuable tool for Java developers.
Lombok val vs var
Post Date: 18 Oct 2023
Lombok has gained immense popularity among Java developers for its ability to simplify coding practices by reducing boilerplate code. In the vast ocean of features offered by Lombok, two features stand out: val and var. In this deep dive, we’ll uncover the secrets of these two variables and demonstrate their utility.
Lombok Test Coverage
Post Date: 21 Jun 2023
When it comes to software development, testing plays a crucial role in ensuring the quality and reliability of the codebase. Test coverage, in particular, is a metric that measures the extent to which the source code of a program has been tested.
Lombok Disadvantages
Post Date: 21 Jun 2023
In the world of Java development, optimizing code efficiency and reducing boilerplate is a constant pursuit. To address these challenges, various tools and libraries have emerged, and one such popular option is Lombok—a Java library that offers annotations for code generation, resulting in benefits such as time-saving and code simplification. However, as with any tool, there are both advantages and drawbacks to consider.
Lombok @Singular Annotation
Post Date: 01 May 2023
Lombok is a Java library that provides a set of annotations and utility classes to reduce boilerplate code in Java projects. One of its popular annotations is @Singular, which generates builder methods for collections.
Java • Avoid Getters & Setters Methods With Lombok
Post Date: 04 Apr 2023
This article provides an overview of how to avoid the repetitive code associated with writing getter and setter methods in Java classes using Lombok. By using Lombok’s annotations, such as @Getter and @Setter, developers can generate these methods automatically, thereby reducing the amount of boilerplate code required in their classes.
Lombok • @AllArgsConstruct vs @RequiredArgsConstructor
Post Date: 30 Mar 2023
What is the difference between @AllArgsConstruct and @RequiredArgsConstructor in Lombok? The main difference between @AllArgsConstructor and @RequiredArgsConstructor is in the fields that are included in the generated constructor.
Lombok • An Overview
Post Date: 13 Mar 2023
Lombok is a Java library that provides annotations to help reduce boilerplate code and increase developer productivity. By using Lombok annotations, Java developers can automatically generate common code such as getters, setters, constructors, equals and hashCode methods, and more.
Lombok • Using @With Annotation to Clone Immutable Objects
Post Date: 29 Aug 2022
Using Lombok’s @With Annotation to Clone Immutable Objects is a beneficial feature that helps developers minimize code replication and ceremonial code. It is the next best alternative to Copy Constructs in object-oriented programming. The @With annotation also requires @AllArgsConstructor to function correctly.
Lombok • Builders and Copy Constructors
Post Date: 27 Dec 2021
Lombok’s builder and copy constructor feature using @Builder is a mechanism that allows the implementation of the Builder Pattern and Copy Constructors in Object-Oriented Programming. This article further illustrates how Lombok mitigates the disadvantages of creating builder methods and copy constructors making Lombok’s @Builder a good foundation for design and code maintainability.