Lombok Unleashed_ Elevating Java Efficiency with Getters, Setters, Constructors, Builders, and More.pdf
Boost Java efficiency with Lombok! Simplify your code by automating getters, setters, constructors, and more, reducing boilerplate and improving readability.
Java is oneof the best languages when it comes to “Object-Oriented
Programming”. But it also faces some major drawbacks, such as code
complexity and overuse of the same codes repeatedly. This doesn’t add
any value to our programs, and at the same time, it is very
time-consuming for the coders too. So, to overcome this major
drawback of Java, “Project Lombok” comes into play, providing Lombok
builders and constructors to further simplify code.
Introduction to Project Lombok
Project Lombok is a popular Java library that aims to reduce boilerplate
code and enhance coders productivity by saving lots of time and their
energy by providing annotations to automatically generate common
Java code during compile time.
What is Project Lombok ?
● Project Lombok addresses the verbosity of Java by offering
annotations that eliminate the need for manually writing repetitive
code constructs such as getters, setters, constructors, equals,
hashCode, and toString methods. By annotating fields or classes
with Lombok annotations, coders can instruct the compiler to
generate these methods automatically, reducing the amount of
boilerplate code and making Java classes more compact and
readable.
Why are we using Project Lombok ?
● Using Project Lombok in Java offers several compelling benefits
that contribute to improved productivity, code quality, and
maintainability.
● Here are a few reasons to choose Project Lombok.
○ It reduces the “Boilerplate Code”.
3.
○ It alsoimproves codes reusability and readability.
○ It is very simple to implement and doesn’t have any
complexity.
○ Integrates easily with “IDEs”.
How to implement Lombok in Java on a Maven project
Most of our projects are based on Maven. So, we just have to add
“Project Lombok” dependencies to our “Pom.xml” file present in our
project.
Go to maven repository and copy Lombok Maven repository from there,
add the latest lombok dependency in your “Pom.xml” and save it, then
refresh the project.
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>
Getters, Setters feature of Project Lombok in Java
4.
In Java, byfar the most common practice is to add getters and setters
using the “Java Beans” pattern. Most of the IDEs automatically generate
code for these patterns.
Let us see the code understand this approach by creating getter and
setter with the help of “Data Objects” and “Data Factory” :
Data Object without Lombok
package org.example.dataobjects;
public class Profile {
private String firstName;
private String lastName;
private String designation;
private int age;
// Getter method for 'firstName' field
public String getFirstName() {
return firstName;
}
// Setter method for 'firstName' field
public void setFirstName(String firstName) {
this.firstName = firstName;
}
// Getter method for 'lastName' field
5.
public String getLastName(){
return lastName;
}
// Setter method for 'lastName' field
public void setLastName(String lastName) {
this.lastName = lastName;
}
// Getter method for 'designation' field
public String getDesignation() {
return designation;
}
// Setter method for 'designation' field
public void setDesignation(String designation) {
this.designation = designation;
}
// Getter method for 'age' field
public int getAge() {
return age;
}
// Setter method for 'age' field
public void setAge(int age) {
6.
this.age = age;
}
}
Whilethe traditional JavaBeans approach for creating getter and setter
methods manually gets the job done, but it has several drawbacks and
limitations that make it less desirable, especially in modern Java
development environments all, its drawbacks are majorly covered in the
Lombok.
So, instead of this, we prefer to use the Lombok pattern. Here is how it
can be implemented in Java :
Data Object with Lombok
package org.example.dataobjects;
//importing getter and setter
import lombok.Getter;
import lombok.Setter;
//getter and setter annotations
@Getter
@Setter
public class Profile {
private String firstName;
private String lastName;
7.
private String designation;
privateint age;
}
Here is how we are setting data and for both approaches, the process is
the same :
package org.example.datafactory;
import org.example.dataobjects.Profile;
public class ProfileData {
public Profile getProfileData() {
Profile profile =new Profile();
// Setting data for 'firstName' field
profile.setFirstName("Parth");
// Setting data for 'lastName' field
profile.setLastName("Kathrotiya");
// Setting data for 'age' field
profile.setAge(23);
// Setting data for 'designation' field
profile.setDesignation("QA Automation Engineer");
return profile;
}
8.
}
Constructor features ofProject Lombok in Java
Constructors without Lombok we have to manually define each
constructor, which can be tedious and error-prone, especially for
classes with many fields. Additionally, we need to handle various
constructor configurations, which can increase the complexity of the
code.
Lombok simplifies this process with @NoArgsConstructor,
@AllArgsConstructor and @RequiredArgsConstructor annotations.
Constructors without Lombok
package org.example.dataobjects;
public class Profile {
private String firstName;
private String lastName;
private String designation;
private int age;
// No-argument constructor
public Profile() {
}
// Constructor with all fields
public Profile(String firstName, String lastName, String designation,
int age) {
9.
this.firstName = firstName;
this.lastName= lastName;
this.designation = designation;
this.age = age;
}
// Constructor without 'age' field
public Profile(String firstName, String lastName, String designation) {
this.firstName = firstName;
this.lastName = lastName;
this.designation = designation;
}
}
Using Lombok annotations reduces the amount of boilerplate code that
needs to be written manually. With Lombok, you simply annotate the
class and fields, and the constructors are generated automatically based
on the specified criteria. This leads to cleaner and more concise code.
Constructors with Lombok
package org.example.dataobjects;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.NonNull;
10.
import lombok.RequiredArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
public classProfile {
private String firstName;
private String lastName;
private String designation;
@NonNull private int age;
}
Various Lombok features and properties
1. ToString Generation
● In Java, toString() is a method defined in the java.lang.Object
class, which serves the purpose of returning a string
representation of an object. The toString() method is inherited by
all classes in Java, and its default implementation in the Object
class returns a string containing the class name followed by the
“at” symbol (@) and the hexadecimal representation of the object’s
hash code.
● However, the default implementation of toString() provided by
Object may not always be meaningful or useful for specific
classes. Therefore, it is common practice for developers to
override the toString() method in their own classes to provide a
custom string representation that better describes the state or
properties of the object.
11.
● As perour example, a Profile class might override toString() to
return a string containing the firstName, lastName, designation,
age information. Overriding toString() allows to easily print or log
object information in a human-readable format, which can be
helpful for debugging, logging, or displaying information to users.
● Without using ToString Lombok annotations we’ve to manually
implement the toString() method within the Profile class. We
concatenate the firstName, lastName, designation, and age fields
to create the desired string representation. This manual
implementation achieves the same result as Lombok’s @ToString
annotation.
Without using ToString Annotations feature
READ THE FULL BLOG: https://tinyurl.com/bddb9cxb