Thursday, May 2, 2024
HomeJava10 Examples Of Lombok Libarary In Java

10 Examples Of Lombok Libarary In Java


 

10 Examples Of Challenge Lombok Annotations In Java

Proper
now, Java is likely one of the hottest programming languages within the
world. Challenge Lombok is principally a Java library software that can be utilized
to attenuate and even take away all of the boilerplate code and thus make the
builders’ job simpler throughout growth. It may also be used to
improve the readability of the supply code, thus permitting you to avoid wasting
area. 

One other vital factor that you just want to bear in mind is
that Lombok provides all these boilerplate codes on the time of compilation
within the ‘.class’ file and never in your supply code. You may have a look at the
following examples to get a greater understanding of what Challenge Lombok
is all about.

1. Java With out Lombok

This
is a vivid instance of Java with out Lombok. It’s a Java mannequin class
with 4 non-public fields in addition to their getters, setters, no-args
constructor, parameterized assemble, and toString methodology. 

public class Worker {

	non-public Integer employeeId;
	non-public String identify;
	non-public String firm;
	non-public String emailId;

	public Worker() {}

	public Worker(Integer employeeId, String identify,
					String firm, String emailId)
	{
		tremendous();
		this.employeeId = employeeId;
		this.identify = identify;
		this.firm = firm;
		this.emailId = emailId;
	}

	public Integer getEmployeeId() { return employeeId; }

	public void setEmployeeId(Integer employeeId)
	{
		this.employeeId = employeeId;
	}

	public String getName() { return identify; }

	public void setName(String identify) { this.identify = identify; }

	public String getCompany() { return firm; }

	public void setCompany(String firm)
	{
		this.firm = firm;
	}

	public String getEmailId() { return emailId; }

	public void setEmailId(String emailId)
	{
		this.emailId = emailId;
	}

	@Override public String toString()
	{
		return "Worker ["
			+ "employeeId=" + employeeId + ", name=" + name
			+ ", "
			+ " company=" + company + ", emailId=" + emailId
			+ "]";
	}
}

2. Java With Lombok

Now
you may have a look at the next instance to see what distinction Lombok
makes to the identical code that’s given above. Please notice that it is rather
straightforward and intuitive to make use of the Lombok annotations.

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Worker {
	non-public @Getter @Setter Integer employeeId;
	non-public @Getter @Setter String identify;
	non-public @Getter @Setter String firm;
	non-public @Getter @Setter String emailId;
}

As
you may see, the distinction is clearly seen. Whereas the primary
instance incorporates nearly 60 strains of code, the second instance reduces it
to round 20 strains with out the assistance of Challenge Lombok. It additionally will increase
the readability and maintainability of the supply code. 

3. Configuring Lombok To Eclipse

You
can simply obtain the Lombok set up file from the official website.
Open the file to start out the set up. You have to specify a location
and choose the IDE. Click on on the set up button to finish the
set up. 

Lastly, you may confirm the set up by going to the Eclipse IDE Assist part. 

4. Lombok Annotations: @Getter and @Setter

Lombok
provides you a particular set of annotations that may make your coding life
simpler. These annotations gives you the getter and setter strategies
for a specified subject. It may also be used at each ranges. 

The Getter annotation will generate a getter methodology together with an entry sort that may merely return the sphere. 

The Setter annotation will generate a setter methodology together with an entry sort that’s public which returns void. 

import lombok.Getter;
import lombok.Setter;

public class Worker {
	non-public @Getter @Setter Integer employeeId;
	non-public @Getter @Setter String identify;
	non-public @Getter @Setter String firm;
	non-public @Getter @Setter String emailId;
}
10 Examples Of Lombok In Java

5. Lombok The @NoArgsConstructor Instance

This
annotation can be utilized to generate a constructor with no arguments. It
principally doesn’t do something and has an empty physique. It’s generally
utilized in mixture with different parameterized constructors. You may
generate an object of a particular class by passing no arguments into the
constructor. 

import lombok.NoArgsConstructor;

@NoArgsConstructor
public class Worker {
	non-public Integer employeeId;
	non-public String identify;
	non-public String firm;
	non-public String emailId;
}

6. Lombok @AllArgsConstructor Instance

This
annotation will generate a parameterized constructor that may settle for a
single parameter for every subject. It would then initialize them utilizing the
parameter. You should utilize it to generate an object of the category by passing
the preliminary values of the sphere into the constructor. 

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class Worker {
	non-public Integer employeeId;
	non-public String identify;
	non-public String firm;
	non-public String emailId;
}

7. Lombok @ToString Instance 

You
can use this annotation to override the toString() methodology in addition to
generate a default implementation. It would print the category identify and the
fields in that order. 

import lombok.ToString;

@ToString
public class Worker {
	non-public Integer employeeId;
	non-public String identify;
	non-public String firm;
	non-public String emailId;
}

8. Lombok @EqualsAndHashCode Instance

This
annotation will override the equals() and hashCode() strategies in addition to
present a defalut implementation. This default implementation will make
use of all of the non-static fields.

import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public class Worker {
	non-public Integer employeeId;
	non-public String identify;
	non-public String firm;
	non-public String emailId;
}


9. Lombok @Information Instance

You
can use this annotation as principally a shortcut annotation that bundles
the entire different annotations like @EqualsAndHashCode, @ToString,
@Getter, and @Setter right into a single annotation. It gives you all of the
generally used boilerplate code within the mannequin lessons of Java. 

import lombok.Information;

@Information
public class Worker {
	non-public Integer employeeId;
	non-public String identify;
	non-public String firm;
	non-public String emailId;
}

10. Lombok @Builder Instance

This
annotation can be utilized to take away all of the boilerplate code that’s
concerned in setting properties for an object. You may set the properties
of an object with a single assertion of code and likewise make it readable
and clear. 

import lombok.Builder;

@Builder
public class Worker {
	non-public Integer employeeId;
	non-public String identify;
	non-public String firm;
	non-public String emailId;
}

That is all about Lombok library in Java. It is one in all my favourite Java library because it make it easier to to jot down clear code and improves developer productiveness. I extremely suggest each Java developer to study Lombok and there’s no higher approach to study then utilizing Lombok in your mission. You can begin utilizing Lombok by following these examples till you get snug. All trendy IDE like Eclipse and IntelliJIDEA help Lombok, so you should use Lombok lessons similar to every other common class in Java. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments