#69. How To Use Project Lombok To Remove Boilerplate code Getter And Setter Methods From POJO Class
Summary
TLDRIn this tutorial, the speaker, Amudh, introduces Lombok, a Java library that automates the creation of boilerplate code such as getters and setters. By demonstrating the manual process of generating these methods in a POJO class, the video highlights the tediousness of this task. The speaker then shows how to integrate Lombok into a Maven project, explaining the installation of the Lombok plugin for IDEs like Eclipse. After installation, the video illustrates how Lombok annotations can be used to automatically generate getter and setter methods, streamlining the development process and reducing code redundancy.
Takeaways
- ๐ The video is about using Lombok to reduce boilerplate code in Java POJO classes.
- ๐ Lombok is a library that can automatically generate getters, setters, and other methods when new fields are added or removed from a class.
- ๐ฆ To use Lombok, one needs to add it as a dependency in their Maven project and also install the Lombok plugin in their IDE.
- ๐ The video provides a demonstration of how to add Lombok to a project and use it to automatically generate getters and setters.
- ๐ The script explains the manual process of creating getters and setters before introducing Lombok as a solution.
- ๐ Lombok annotations like `@Getter` and `@Setter` can be used to specify the generation of only getters or setters as needed.
- ๐ The video shows how Lombok handles the addition and removal of fields in a class without requiring manual updates to the getter and setter methods.
- ๐ The presenter mentions that Lombok is particularly useful in reducing lines of code and avoiding repetitive tasks.
- ๐ The video script includes instructions on how to install the Lombok plugin for Eclipse IDE, either through the marketplace or by manually downloading the `lombok.jar`.
- ๐ The script also mentions that Lombok will be covered in more depth in future videos, including its builder feature for creating JSON bodies.
- ๐ข The video concludes with an invitation for viewers to comment, like, subscribe, and share if they found the content helpful.
Q & A
What is the main purpose of the Lombok library discussed in the video?
-The main purpose of the Lombok library is to eliminate the need for manually creating boilerplate code such as getters and setters in Java classes, thus reducing the lines of code and simplifying the development process.
How does Lombok help in reducing boilerplate code when creating POJO classes?
-Lombok provides annotations that automatically generate getters, setters, and other methods at compile time, so developers do not need to write these methods manually for each field in a class.
What is the process of adding Lombok as a dependency to a Maven project?
-To add Lombok as a dependency to a Maven project, one needs to search for 'lombok' in the Maven Central Repository, find the latest version, and copy the dependency into the project's pom.xml file.
Why is it necessary to install the Lombok plugin in the IDE after adding the Lombok dependency?
-The Lombok plugin is necessary in the IDE to recognize the Lombok annotations and generate the corresponding methods (getters, setters, etc.) during the development process, without requiring explicit method definitions.
What are the two ways to install the Lombok plugin in Eclipse mentioned in the video?
-The two ways to install the Lombok plugin in Eclipse are by downloading the lombok.jar and running it, which opens an installer window, or by using the Eclipse Marketplace if available and working in the user's network environment.
How can the Lombok plugin be installed via the command prompt?
-The Lombok plugin can be installed via the command prompt by navigating to the directory containing the lombok.jar file, and then running the command 'java -jar lombok.jar'. This will launch the Lombok installer window, where the user can specify the IDE installation path and install the plugin.
What is the advantage of using Lombok when adding or removing fields in a class?
-The advantage of using Lombok is that it automatically handles the addition or removal of getters and setters for new or removed fields, without the developer needing to manually update the method definitions.
Can Lombok be used without other dependencies like Jackson in a project?
-Yes, Lombok can be used independently of other dependencies like Jackson. The video mentions Jackson as part of a reactive tutorial, but Lombok's primary function is to reduce boilerplate code and does not require Jackson or any other specific libraries.
What is the 'Builder' pattern mentioned in the video, and how is it related to Lombok?
-The 'Builder' pattern is a design pattern used to create objects step by step, often used in frameworks to construct JSON bodies. Lombok provides an annotation for the Builder pattern, which simplifies object construction and will be discussed in a subsequent video.
What should developers do if they face issues with the Lombok plugin installation via the Eclipse Marketplace?
-If developers face issues with the Lombok plugin installation via the Eclipse Marketplace, they can download the lombok.jar file directly from the Lombok website or the Maven Central Repository and install it manually using the command prompt or by running the jar file.
How can the Lombok library help in reducing the development time for creating POJO classes?
-Lombok library can help in reducing the development time by automatically generating the necessary boilerplate code like getters and setters, allowing developers to focus on the core logic of the application rather than repetitive code tasks.
Outlines
๐ Introduction to Lombok for Reducing Boilerplate Code
The speaker, Amudh, introduces the video's topic: Lombok, a Java library designed to reduce boilerplate code in POJO classes. The speaker explains how they previously had to manually generate getters and setters for each field in their classes, which was time-consuming and error-prone. They provide an example of creating a simple 'Employee' class with private fields for first name, last name, and age, and demonstrate the manual process of generating JSON from this class. The speaker then explains the benefits of using Lombok to automatically generate these methods when fields are added or removed, thus simplifying the development process.
๐ ๏ธ Installing Lombok Plugin for IDEs to Automate Code Generation
This paragraph details the process of installing the Lombok plugin in an Integrated Development Environment (IDE) to automate the generation of getters and setters. The speaker discusses two methods of installation: downloading the Lombok JAR file and running it, or using the Eclipse Marketplace (though they note potential issues with this method). They provide step-by-step instructions for installing the plugin manually, including running the JAR file as an administrator to avoid access issues. The speaker emphasizes the need to restart the IDE after installation to ensure the plugin is active and can generate the necessary methods automatically.
๐ Demonstrating Lombok's Automatic Getters and Setters in Action
The speaker demonstrates the functionality of Lombok after the plugin installation. They show how, with Lombok annotations added to the 'Employee' class, the IDE now automatically provides getters and setters without the need for manual coding. The speaker modifies the class by adding a 'salary' field and shows that Lombok automatically generates the corresponding getter and setter for this new field. They also show how removing a field, such as 'age', no longer requires manually removing the associated getter and setter. The video concludes with a brief mention of Lombok's 'Builder' feature, which will be covered in a future tutorial, and the speaker invites viewers to comment, like, and subscribe for more content.
Mindmap
Keywords
๐กLombok
๐กPOJO (Plain Old Java Object)
๐กGetters and Setters
๐กJSON
๐กMaven
๐กAnnotations
๐กEclipse
๐กIDE (Integrated Development Environment)
๐กBoilerplate Code
๐กBuilder
Highlights
Introduction to Lombok, a library to reduce boilerplate code in Java.
Explanation of the manual process of generating getters and setters in POJO classes.
Demonstration of creating a package and a POJO class called 'Employee'.
Description of the need for getters and setters to convert a POJO class into JSON.
Manual generation of getters and setters using the IDE's features.
The inconvenience of updating boilerplate code when fields are added or removed.
Usage example of the 'Employee' class with manual setter methods.
Introduction of Lombok as a solution to automatically handle getter and setter generation.
Instructions on adding Lombok as a Maven dependency from the Maven Central Repository.
Clarification that Lombok is not required for JSON processing but is used for code simplification.
Explanation of Lombok annotations that replace the need for manual getter and setter methods.
The necessity of installing the Lombok plugin in the IDE for the annotations to work.
Different methods to install the Lombok plugin, including direct download and Eclipse Marketplace.
Detailed steps for installing the Lombok plugin by downloading the .jar file.
The advantage of Lombok in automatically updating getters and setters when fields are modified.
Demonstration of using Lombok in an IDE after plugin installation, showing the availability of getters and setters.
The impact of Lombok on reducing lines of code and simplifying code maintenance.
Preview of the next topic in the series: Lombok's Builder pattern for creating JSON bodies.
Invitation for viewers to comment, like, subscribe, and share the video for further support.
Transcripts
hello everyone amudh here from the
target common youtube channel
and today i am going to explain you
about the lombok so in recessive
tutorials i have created so many pojo
classes and if you remember that i was
generating the
getters and status method
manually right
so let me go through with an example
again here let me create one package
i will simply name it like pojo classes
and
i will
create a pojo class okay
so suppose i have
one class called employee and employee
will have some field state like
private
string
first name
private
string
last name
then private
in
age
and
yeah that is it would be only three
so
if you want to create the json body out
of this puzzle class side so we need to
set the value first right and then we
used to convert it
into json so that we can use that as a
payload
so for that since these variables are
private right so obviously we need a get
a setup method to set the value okay and
get emitted required to convert that to
json okay
so for that what i used to do i used to
go to source and then generate and
gather sensitives
select all and generate it
okay
so if you see here whatever code was
generated
from line number 9 to 26 it is actually
boilerplate code
okay so if you are going to have any new
fields again need to go and generate it
right if you remove any field then again
it to go and delete those unwanted gate
and set method for that property right
so these general boilerplate code and
extra headache while removing and adding
fields right so let me show you one
usage also so i will create another
class
and i will put
usage of employee
so let me get a main method so what i'm
going to do i'm going to
create
employee class object first and
to set the property right what i need to
do we need to call the
setup method
right set a set first name
okay
then
employee
dot set
last name
okay
and then
employee
dot set is third 31
okay
so this i was able to do because i have
the setter method and this is again
boilerplate code so actually we have
one library which can help us to
remove these things and it will
automatically take care when you remove
or add any new field
okay or you remove any field or you add
a new field there will no need to
explicitly generate the getternet setter
for it
okay and that library which can help you
in this is called lombok
okay
so simply go to maven center repository
and search for lombok here
okay there you go you see the project
lombok click on it and get the latest
version and copy the dependency
okay and i have already created one
maven project simple main project where
i have dependencies of uh
recession and jackson okay you simply
add it here
okay
you don't require to have this raises
your injection dependency to use lombok
no
these are just i added because this is
recessive tutorial and so i added these
things okay but you simply can also have
lombok project lombok dependency to
practice
okay so let me format it
okay and let me simply remove the scope
okay
let me remove the scope
and save it
okay
so lombok will be added
ah as a dependency for my project
now what i can do instead of generating
this getter and setter method
lombok provide different annotations
actually provides a lot of annotations
but what we are going to use here if we
have one two
okay so give me let me give you one more
thing here like suppose we do the source
right we go to source and
click on generations getters and setters
so we have option like whether we want
to only generate the getters or setters
or both right
same thing lombok provides you some
rotation like suppose you want to
generate
only getters
okay so you can simply type add
symbol and then type getter
okay so this will come from your lombok
okay
and also if you want settle method as
well so just simply type
setter
ok
so
we can see
and i am not going to generate any data
and saturn now just i put this
annotation so now you may ask okay what
is the advantage
okay so if i go to my
usage of employee class okay and we can
see for everything it is giving error
now
set set is not uh it is not able to find
setup method for first name last name or
age
okay let me type able to get the get
method
simpler dot gate that is also not coming
here
okay the region the reason behind it we
have added the lombok dependency in the
project right but to help with the gates
and status means you should see them in
season you need to add
lombok as a plugin as well
okay
so there are multiple ways to add that
lombok as a plugin
so if you go here and that depends upon
which
id you are using so if you are using
eclipse
okay
so you simply go to install here you
need to go to project lombok official
website and you see the install right
click on it and then you see
wherever you want to have so suppose if
i want to
add the plugin to eclipse so click on
eclipse here okay so there are two ways
to install the plugin one will be the
first one
okay in which you need to download the
jar file as well you can see
double click let me increase the font
size
okay you can see here double click
lombok.jar
okay so this i will explain so this this
is one option where you need to download
the lombok.jar and another way you have
to download by the marketplace
okay
uh sorry install new software so
generally this
sometimes doesn't work and if you're in
company uh network so obviously this
will not work sometimes like the similar
problem is ng
okay so you may not able to use this one
also and sometimes it's not working
locally as well okay so the best way to
install
lombok plugin is by a wire downloading
the lombok.jar
so from where you will download the
lombok.jar either you can go to this
download section okay you can click on
it or if you go to maven center
repository and where you copied the
dependency you see here the jar under
the file section you see the jar
when you click on it automatically this
will be downloaded
okay
so
what you need to do
ah so first thing what you need to do
here
let me go to
eclipse again
so
ah when you click on the downloaded jar
file automatically it will start some
process so
my
is downloaded here let me double click
on it
okay
so it will as soon as you double click
on it it will open this window and it
will look for
where is your eclipse installed
okay you can see uh eclipse or intellij
both so he can say i cannot find any id
on your computer
okay
so if you
if you have id installed please specify
location so sometime it happens
automatic automatically it is not able
to find where your id is installed so
don't worry we have an option to specify
explicitly where is my
um what you call that
eclipse installed okay our eclipse file
is there
okay
so
but before that what i want to do i i
simply use command prompt okay and i
i will just run as an administrator
sometime happens like it will say that i
am not able to
edit some changes and also better to
start with
um better to start the command prompt
with admin access okay and let me
increase the font size
so properties
then
okay
yeah better
so first thing what you need to do you
need to go to the
uh location where you have the
downloaded the lombok file okay so use
the cd and then paste
okay and here you have command like java
hyphen jar
then give the file name
okay it's like same as double clicking
on
that
that lombok dot jar
this long block and the version is 1.8
1.1824
dot jar
okay
so you can see it is launching the
project lumber installer window again
the same which
which was
launched when you double click on the
jar so same thing you can achieve via
here as well
okay
so again in order to find the
id no problem i know where is my eclipse
installed so i will go to this location
is my eclipse installed i will copy the
path
okay
and go to specify location
and just paste it here okay
and you see install and update
okay simply click on install update and
you can see
the lombok has been installed on your
selected ide
okay
you can see but to
take this in effect you need to restart
your eclipse okay
so
let me quit the installer let me go here
let me close the
eclipse and
the launch it again
okay so simple thing i
open the command prompt as
with the admin access so that i should
not get any access so okay but you can
directly click on the jar file and
specify the location okay if it works
for you then fine if not then start with
the command prompt
okay so i simply i want to cover all the
scenarios which you might face here
okay
so
let me
let me see if i am getting the setter
method or not so employee
dot
dot
can you see i'm getting the settle
method now i was not getting before
installing the plugin but now i am able
to see
so see let me set the first name like
amode
okay
and set
last name
mahajan
okay
and employee dot
set age as
31
right
and again i can print the value water by
set using the get method
okay employee dot get first name and you
can see i am getting all the getters
method here
so let me save and let me run it
okay
so we can see we are getting the value
so using the project lombok we can
eliminate the getters and setters
dependencies sorry that the boilerplate
could and why i like it
more because it will reduce the lines of
code
in your class and also it will
automatically handle like suppose
if i remove this property
okay and instead of that i am adding
something like salary
okay so neither no need to do anything
here with the project number but if you
are not using project lumber then for
this property you need to add the getter
and setter and for the age you need to
remove it right that was the effort you
need to make but here there is no
problem you can use the
if you type the set you will see the
salary
okay
so this is the advantage of project
lombok there is another advantage or
another introduction called builder
which is like the mostly used in your
recession framework to create the json
body
so that i will take in next next video
okay so if you have any doubts regarding
this video please comment on this and if
you really like my video please like
comment subscribe and say with others
thank you everyone
Browse More Related Video
NELAYAN SELAT ALAS & PASIR REKLAMASI - Ekspedisi Indonesia Biru #18
Java Inheritance | Java Inheritance Program Example | Java Inheritance Tutorial | Simplilearn
Stage 5 #4 Number Classes like BigInteger And Javadoc Comments
#4 IoC and DI in Spring
SUPER() in Python explained! ๐ด
How to set up Visual Studio Code for Executing and Debugging C Programs | Tutorial
5.0 / 5 (0 votes)