menu
Lombok Java - Javatpoint
Lombok Java - Javatpoint
Lombok Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.

Lombok Java - Javatpoint

Java is the most popular object-oriented programming language but it has some drawbacks. The major drawback is to write lots of boilerplate code. To overcome this drawback, project Lombok comes into existence. It is a tool that spices up our Java application. In this section, we will discuss the project Lombok, features, Lombok package.

The project Lombok is a popular and widely used Java library that is used to minimize or remove the boilerplate code. It saves time and effort. Just by using the annotations, we can save space and readability of the source code. It is automatically plugging into IDEs and build tools to spice up our Java application.

Here, a question arises that does project Lombok and IDEs do the same work? If yes, then what is the use of Lombok?

The answer is no, IDEs and Lombok do different works but are closely similar to each other. When we use IDEs to generate these boilerplate codes (getters and setters), we save ourselves from writing getters and setters manually but it actually exists in the source code that increases the lines of code, and reduces maintainability and readability. While the project Lombok adds all these boilerplate codes at the compile-time in the class file instead of adding these boilerplate code in original source code.

The Lombok Java API includes the following packages that can be used for different purposes.

Suppose, we are developing a Java application for which a POJO file is required that has several private fields. For these fields, we have to generate getters and setters accessor methods to provide access. Generating getters and setters for each field increases the line of code.

Moreover, adding a constructor and a toString() technique will cause much more lines of code and mess. What about when we are utilizing Java objects that need to be closed after use, so we need to code a finally-block or use try-with-resources to ensure that the object closing occurs. Adding a finally-block boilerplate to close objects can add a significant amount of clutter to the code. Hence, we deal with lots of boilerplate code.

To overcome the same problem, the project Lombok comes into existence.

The package contains all the annotations and classes required to use Lombok. All other packages are only applicable to those who are extending Lombok for their own uses, except the following two packages:

The Java Lombok package contains the following classes, and annotations.

There are several reasons to use Lombok but some of them are as follows:

Check for Nulls

It is the most basic utility that Lombok offers. The library offers @NonNull annotation that can be used to generate a null check on a setter field. It throws the NullPointerException if the annotated class field contains a null value. Note that we cannot annotate the primitive parameter. With @NonNull annotation. For example, consider the following code snippet.

The above code is the same as:

Generating getters and setters can be laborious work if there are several private fields in the POJO file. The task can be performed easily with the Lombok by using the @Getter and @Setter annotation. For example, consider the following code.

Without Project Lombok:

Using Project Lombok:

We observe that the code becomes more concise and less error-prone. Note that @Getter and @Setter annotation also accept an optimal parameter to designate the access level if needed. One of the benefits is that it takes care of the naming convention. For example, it generates an accessor method for the boolean field that begins with is instead of get. If they are applied at the class level, getters and setters are generated for each non-static field within the class.

The @Data annotation can be used to apply usefulness behind all the annotations. It means that annotate a class with the @Data annotation, Lombok produces getters and setters for every one of the non-static class fields and a class constructor. It is the same as toString(), equivalents(), and hashCode() strategies. It makes the coding of a POJO exceptionally simple.

Generate Constructor Automatically

It provides two annotations to generate constructors i.e. @AllArgsConstructor and @NoArgsConstructor. The @AllArgsConstructor annotation generates a constructor with all fields that are declared. If any field is added or removed, the constructor is also revised for the changes. The @NoArgsConstructor annotation simply generates the constructor without any argument.

Generating Getters for Final Fields

The @Value annotation is the same as the @Data annotation. It is a class-level annotation. The only difference is that it generates an immutable class. It invokes the automatic generation of getters only for all private and final fields. Note that it does not generate setters for any field, and marked the class as final.

To configure the Lombok project in Eclipse IDE, follow the steps given below:

Step 1: First, download the lombok.jar file.

Step 2: For executing the above JAR file, double-click on the downloaded JAR file. A GUI appears on the screen in which we have to specify the IDE on which we want to configure the Lombok project.

Step 3: Click on the Specify location button and browse the directory in which Eclipse IDE is installed. From the folder select eclipse.exe file.

Step 4: Click on the Install/ Update button.

Once the above process is completed check project Lombok is installed successfully or not.

Step 5: Open eclipse IDE -> Help -> About Eclipse IDE. If the project Lombok is listed, means that properly installed.

Step 6: At last, click on the Close button.

The project Lombok is successfully integrated with the Eclipse IDE.

First, we will create a Java program without using Lombok to see the differences.

Student.java

Let's consider the above code with Lombok.

In the following program, we have used the commonly used Lombok annotation to make our code concise.

Employee.java

Consider both the above codes, we can clearly see the differences. Using Lombok reduces the line of code, makes the code concise, readable, and less error-prone.

Digital Marketing

Elasticsearch

Entity Framework

Firewall

Functional Programming

Google Colab

Graph Theory

Groovy

Group Discussion

Informatica

Ionic

ITIL

IOS with Swift

Angular Material

Deep Learning

Aptitude

Reasoning

Verbal Ability

Interview Questions

Company Questions

Artificial Intelligence

AWS

Selenium

Cloud Computing

Hadoop

ReactJS

Data Science

Angular 7

Blockchain

Git

Machine Learning

DevOps

DBMS

Data Structures

DAA

Operating System

Computer Network

Compiler Design

Computer Organization

Discrete Mathematics

Ethical Hacking

Computer Graphics

Software Engineering

Web Technology

Cyber Security

Automata

C Programming

C++

Java

.Net

Python

Programs

Control System

Data Mining

Data Warehouse

Website Development

Android Development

Website Designing

Digital Marketing

Summer Training

Industrial Training

College Campus Training

Address: G-13, 2nd Floor, Sec-3

Noida, UP, 201301, India

Contact No: 0120-4256464, 9990449935

© Copyright 2011-2021 www.javatpoint.com. All rights reserved. Developed by JavaTpoint.