#69. How To Use Project Lombok To Remove Boilerplate code Getter And Setter Methods From POJO Class

Retarget Common
8 May 202214:14

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

00:00

😀 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.

05:01

🛠️ 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.

10:03

🔄 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

Lombok is a Java library that automatically plugs into the IDE and generates boilerplate code such as getters, setters, constructors, toString, hashCode, and equals methods at compile time. In the video, the speaker explains how Lombok can be used to eliminate the need for manually writing these repetitive code blocks, thereby reducing the lines of code and simplifying the development process.

💡POJO (Plain Old Java Object)

A POJO is a simple Java class that does not have any specific framework or library dependencies. In the context of the video, the speaker creates a POJO class called 'Employee' with private fields and demonstrates how Lombok can be used to automatically generate the necessary methods for such a class, which is a common practice in Java development.

💡Getters and Setters

Getters and setters are methods used in Java to access and modify the values of private fields within a class. They are part of the standard Java Bean convention. The video script discusses how the manual creation of these methods can be tedious and how Lombok can automate this process, improving developer productivity.

💡JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. In the script, the speaker mentions creating a JSON body from the 'Employee' class, which is a common task in web development for sending data over the internet.

💡Maven

Maven is a build automation tool used primarily for Java projects, which manages project builds, dependencies, and documentation. The video describes how to add Lombok as a dependency in a Maven project by searching for it in the Maven Central Repository and copying the dependency into the project's 'pom.xml' file.

💡Annotations

In Java, annotations are a form of metadata that can be added to Java code elements. Lombok uses annotations to indicate the code generation it should perform. For example, the '@Getter' and '@Setter' annotations from Lombok tell the compiler to generate the corresponding getter and setter methods for a class's fields.

💡Eclipse

Eclipse is an integrated development environment (IDE) used in Java development. The video script provides instructions on how to install the Lombok plugin for Eclipse, which is necessary for the IDE to recognize Lombok annotations and generate the associated code.

💡IDE (Integrated Development Environment)

An IDE is a software application that provides comprehensive facilities to computer programmers for software development. The video mentions Eclipse and IntelliJ as examples of IDEs where Lombok can be installed as a plugin to enhance developer experience by automating code generation.

💡Boilerplate Code

Boilerplate code refers to sections of code that have to be included in many places with little or no modification. In the video, the speaker discusses the repetitive nature of writing getters and setters and how Lombok helps in eliminating this boilerplate by generating it automatically.

💡Builder

The Builder pattern is a software design pattern used to construct complex objects step by step. In the context of the video, the speaker mentions the Builder pattern as a feature of Lombok that is particularly useful for creating JSON bodies, which will be covered in a future tutorial.

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

play00:00

hello everyone amudh here from the

play00:02

target common youtube channel

play00:04

and today i am going to explain you

play00:06

about the lombok so in recessive

play00:08

tutorials i have created so many pojo

play00:11

classes and if you remember that i was

play00:14

generating the

play00:15

getters and status method

play00:17

manually right

play00:19

so let me go through with an example

play00:22

again here let me create one package

play00:28

i will simply name it like pojo classes

play00:32

and

play00:33

i will

play00:35

create a pojo class okay

play00:39

so suppose i have

play00:41

one class called employee and employee

play00:43

will have some field state like

play00:45

private

play00:48

string

play00:49

first name

play00:51

private

play00:53

string

play00:55

last name

play00:57

then private

play00:58

in

play00:59

age

play01:01

and

play01:02

yeah that is it would be only three

play01:05

so

play01:06

if you want to create the json body out

play01:08

of this puzzle class side so we need to

play01:10

set the value first right and then we

play01:13

used to convert it

play01:14

into json so that we can use that as a

play01:16

payload

play01:18

so for that since these variables are

play01:20

private right so obviously we need a get

play01:23

a setup method to set the value okay and

play01:26

get emitted required to convert that to

play01:30

json okay

play01:32

so for that what i used to do i used to

play01:34

go to source and then generate and

play01:37

gather sensitives

play01:39

select all and generate it

play01:42

okay

play01:44

so if you see here whatever code was

play01:46

generated

play01:47

from line number 9 to 26 it is actually

play01:50

boilerplate code

play01:52

okay so if you are going to have any new

play01:54

fields again need to go and generate it

play01:56

right if you remove any field then again

play01:59

it to go and delete those unwanted gate

play02:01

and set method for that property right

play02:04

so these general boilerplate code and

play02:05

extra headache while removing and adding

play02:08

fields right so let me show you one

play02:11

usage also so i will create another

play02:14

class

play02:15

and i will put

play02:17

usage of employee

play02:24

so let me get a main method so what i'm

play02:26

going to do i'm going to

play02:28

create

play02:29

employee class object first and

play02:31

to set the property right what i need to

play02:33

do we need to call the

play02:35

setup method

play02:36

right set a set first name

play02:40

okay

play02:42

then

play02:43

employee

play02:44

dot set

play02:47

last name

play02:50

okay

play02:50

and then

play02:52

employee

play02:53

dot set is third 31

play02:57

okay

play02:58

so this i was able to do because i have

play03:00

the setter method and this is again

play03:02

boilerplate code so actually we have

play03:07

one library which can help us to

play03:09

remove these things and it will

play03:11

automatically take care when you remove

play03:14

or add any new field

play03:16

okay or you remove any field or you add

play03:18

a new field there will no need to

play03:20

explicitly generate the getternet setter

play03:23

for it

play03:24

okay and that library which can help you

play03:27

in this is called lombok

play03:29

okay

play03:30

so simply go to maven center repository

play03:33

and search for lombok here

play03:37

okay there you go you see the project

play03:40

lombok click on it and get the latest

play03:43

version and copy the dependency

play03:46

okay and i have already created one

play03:48

maven project simple main project where

play03:50

i have dependencies of uh

play03:52

recession and jackson okay you simply

play03:55

add it here

play03:57

okay

play03:58

you don't require to have this raises

play04:00

your injection dependency to use lombok

play04:02

no

play04:04

these are just i added because this is

play04:05

recessive tutorial and so i added these

play04:08

things okay but you simply can also have

play04:11

lombok project lombok dependency to

play04:14

practice

play04:15

okay so let me format it

play04:18

okay and let me simply remove the scope

play04:22

okay

play04:24

let me remove the scope

play04:26

and save it

play04:29

okay

play04:30

so lombok will be added

play04:33

ah as a dependency for my project

play04:36

now what i can do instead of generating

play04:39

this getter and setter method

play04:41

lombok provide different annotations

play04:44

actually provides a lot of annotations

play04:46

but what we are going to use here if we

play04:48

have one two

play04:50

okay so give me let me give you one more

play04:53

thing here like suppose we do the source

play04:56

right we go to source and

play04:58

click on generations getters and setters

play05:01

so we have option like whether we want

play05:03

to only generate the getters or setters

play05:05

or both right

play05:07

same thing lombok provides you some

play05:10

rotation like suppose you want to

play05:12

generate

play05:13

only getters

play05:14

okay so you can simply type add

play05:18

symbol and then type getter

play05:21

okay so this will come from your lombok

play05:24

okay

play05:25

and also if you want settle method as

play05:28

well so just simply type

play05:30

setter

play05:32

ok

play05:34

so

play05:35

we can see

play05:37

and i am not going to generate any data

play05:39

and saturn now just i put this

play05:40

annotation so now you may ask okay what

play05:43

is the advantage

play05:45

okay so if i go to my

play05:48

usage of employee class okay and we can

play05:50

see for everything it is giving error

play05:52

now

play05:53

set set is not uh it is not able to find

play05:56

setup method for first name last name or

play05:59

age

play06:00

okay let me type able to get the get

play06:03

method

play06:04

simpler dot gate that is also not coming

play06:07

here

play06:08

okay the region the reason behind it we

play06:11

have added the lombok dependency in the

play06:13

project right but to help with the gates

play06:16

and status means you should see them in

play06:19

season you need to add

play06:22

lombok as a plugin as well

play06:24

okay

play06:25

so there are multiple ways to add that

play06:28

lombok as a plugin

play06:29

so if you go here and that depends upon

play06:31

which

play06:33

id you are using so if you are using

play06:35

eclipse

play06:37

okay

play06:38

so you simply go to install here you

play06:40

need to go to project lombok official

play06:42

website and you see the install right

play06:44

click on it and then you see

play06:47

wherever you want to have so suppose if

play06:49

i want to

play06:50

add the plugin to eclipse so click on

play06:52

eclipse here okay so there are two ways

play06:55

to install the plugin one will be the

play06:58

first one

play06:59

okay in which you need to download the

play07:02

jar file as well you can see

play07:04

double click let me increase the font

play07:06

size

play07:08

okay you can see here double click

play07:10

lombok.jar

play07:12

okay so this i will explain so this this

play07:15

is one option where you need to download

play07:17

the lombok.jar and another way you have

play07:21

to download by the marketplace

play07:23

okay

play07:24

uh sorry install new software so

play07:27

generally this

play07:28

sometimes doesn't work and if you're in

play07:30

company uh network so obviously this

play07:32

will not work sometimes like the similar

play07:34

problem is ng

play07:36

okay so you may not able to use this one

play07:38

also and sometimes it's not working

play07:40

locally as well okay so the best way to

play07:43

install

play07:44

lombok plugin is by a wire downloading

play07:47

the lombok.jar

play07:49

so from where you will download the

play07:51

lombok.jar either you can go to this

play07:53

download section okay you can click on

play07:56

it or if you go to maven center

play07:58

repository and where you copied the

play08:01

dependency you see here the jar under

play08:03

the file section you see the jar

play08:06

when you click on it automatically this

play08:07

will be downloaded

play08:09

okay

play08:11

so

play08:13

what you need to do

play08:15

ah so first thing what you need to do

play08:17

here

play08:18

let me go to

play08:20

eclipse again

play08:23

so

play08:25

ah when you click on the downloaded jar

play08:27

file automatically it will start some

play08:29

process so

play08:30

my

play08:32

is downloaded here let me double click

play08:34

on it

play08:37

okay

play08:38

so it will as soon as you double click

play08:40

on it it will open this window and it

play08:42

will look for

play08:43

where is your eclipse installed

play08:46

okay you can see uh eclipse or intellij

play08:49

both so he can say i cannot find any id

play08:52

on your computer

play08:53

okay

play08:54

so if you

play08:56

if you have id installed please specify

play08:58

location so sometime it happens

play09:00

automatic automatically it is not able

play09:02

to find where your id is installed so

play09:04

don't worry we have an option to specify

play09:08

explicitly where is my

play09:11

um what you call that

play09:13

eclipse installed okay our eclipse file

play09:15

is there

play09:16

okay

play09:17

so

play09:18

but before that what i want to do i i

play09:21

simply use command prompt okay and i

play09:25

i will just run as an administrator

play09:27

sometime happens like it will say that i

play09:29

am not able to

play09:31

edit some changes and also better to

play09:33

start with

play09:36

um better to start the command prompt

play09:38

with admin access okay and let me

play09:41

increase the font size

play09:42

so properties

play09:44

then

play09:46

okay

play09:48

yeah better

play09:49

so first thing what you need to do you

play09:50

need to go to the

play09:52

uh location where you have the

play09:54

downloaded the lombok file okay so use

play09:57

the cd and then paste

play09:59

okay and here you have command like java

play10:03

hyphen jar

play10:05

then give the file name

play10:07

okay it's like same as double clicking

play10:09

on

play10:10

that

play10:11

that lombok dot jar

play10:13

this long block and the version is 1.8

play10:17

1.1824

play10:20

dot jar

play10:22

okay

play10:24

so you can see it is launching the

play10:26

project lumber installer window again

play10:29

the same which

play10:30

which was

play10:32

launched when you double click on the

play10:34

jar so same thing you can achieve via

play10:36

here as well

play10:37

okay

play10:38

so again in order to find the

play10:40

id no problem i know where is my eclipse

play10:43

installed so i will go to this location

play10:45

is my eclipse installed i will copy the

play10:48

path

play10:49

okay

play10:51

and go to specify location

play10:53

and just paste it here okay

play10:56

and you see install and update

play10:59

okay simply click on install update and

play11:02

you can see

play11:05

the lombok has been installed on your

play11:08

selected ide

play11:10

okay

play11:10

you can see but to

play11:12

take this in effect you need to restart

play11:16

your eclipse okay

play11:18

so

play11:18

let me quit the installer let me go here

play11:21

let me close the

play11:24

eclipse and

play11:26

the launch it again

play11:28

okay so simple thing i

play11:31

open the command prompt as

play11:33

with the admin access so that i should

play11:34

not get any access so okay but you can

play11:37

directly click on the jar file and

play11:40

specify the location okay if it works

play11:42

for you then fine if not then start with

play11:45

the command prompt

play11:46

okay so i simply i want to cover all the

play11:49

scenarios which you might face here

play11:52

okay

play11:53

so

play11:55

let me

play11:59

let me see if i am getting the setter

play12:02

method or not so employee

play12:04

dot

play12:07

dot

play12:08

can you see i'm getting the settle

play12:10

method now i was not getting before

play12:13

installing the plugin but now i am able

play12:15

to see

play12:16

so see let me set the first name like

play12:19

amode

play12:20

okay

play12:22

and set

play12:25

last name

play12:26

mahajan

play12:29

okay

play12:30

and employee dot

play12:33

set age as

play12:36

31

play12:37

right

play12:38

and again i can print the value water by

play12:41

set using the get method

play12:43

okay employee dot get first name and you

play12:46

can see i am getting all the getters

play12:48

method here

play12:49

so let me save and let me run it

play12:54

okay

play12:56

so we can see we are getting the value

play12:58

so using the project lombok we can

play13:01

eliminate the getters and setters

play13:04

dependencies sorry that the boilerplate

play13:07

could and why i like it

play13:10

more because it will reduce the lines of

play13:12

code

play13:13

in your class and also it will

play13:15

automatically handle like suppose

play13:18

if i remove this property

play13:20

okay and instead of that i am adding

play13:23

something like salary

play13:25

okay so neither no need to do anything

play13:28

here with the project number but if you

play13:30

are not using project lumber then for

play13:32

this property you need to add the getter

play13:34

and setter and for the age you need to

play13:36

remove it right that was the effort you

play13:39

need to make but here there is no

play13:41

problem you can use the

play13:43

if you type the set you will see the

play13:45

salary

play13:47

okay

play13:49

so this is the advantage of project

play13:51

lombok there is another advantage or

play13:53

another introduction called builder

play13:54

which is like the mostly used in your

play13:58

recession framework to create the json

play14:00

body

play14:01

so that i will take in next next video

play14:04

okay so if you have any doubts regarding

play14:07

this video please comment on this and if

play14:09

you really like my video please like

play14:11

comment subscribe and say with others

play14:13

thank you everyone

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
JavaLombokPOJOTutorialsCode SimplificationGetters and SettersJSONMavenEclipseIDE Plugins
هل تحتاج إلى تلخيص باللغة الإنجليزية؟