KapreSoft
Thank you for unblocking ads; your support allows us to continue delivering free, high-quality content that truly matters to you.

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
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
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
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.
Cleaner Code and Enhanced Testability: Harnessing the Power of Lombok Builders
In the realm of Java development, the quest for cleaner code and improved testability is ever-present. One formidable ally in this quest is Project Lombok, a mature library that revolutionizes the way Java developers handle boilerplate code.
Lombok val vs var
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
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
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
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
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
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
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 • Exclude Generated Code From Test Coverage
When using Lombok in a Java project, the library generates code at compile-time, such as getters, setters, constructors, equals and hashCode methods, and more.
Lombok • Using @With Annotation to Clone Immutable Objects
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
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.