Anyway, this article is not about Record, which also provide similar benefit, not at the scale of Lombok but a lot of them. This article is about Lombok and how to effectively use Lombok by exploring its various feature through live examples. But, before we get to the 10 best examples that will teach you everything you need to know about Lombok in Java, let me tell you a little bit more about what it really is.
10 Examples Of Project Lombok Annotations In Java
Right now, Java is one of the most popular programming languages in the world. Project Lombok is basically a Java library tool that can be used to minimize or even remove all the boilerplate code and thus make the developers' job easier during development. It can also be used to increase the readability of the source code, thus allowing you to save space.
Another important thing that you need to keep in mind is that Lombok adds all these boilerplate codes at the time of compilation in the '.class' file and not in your source code. You can look at the following examples to get a better understanding of what Project Lombok is all about.
1. Java Without Lombok
public class Employee { private Integer employeeId; private String name; private String company; private String emailId; public Employee() {} public Employee(Integer employeeId, String name, String company, String emailId) { super(); this.employeeId = employeeId; this.name = name; this.company = company; this.emailId = emailId; } public Integer getEmployeeId() { return employeeId; } public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getEmailId() { return emailId; } public void setEmailId(String emailId) { this.emailId = emailId; } @Override public String toString() { return "Employee [" + "employeeId=" + employeeId + ", name=" + name + ", " + " company=" + company + ", emailId=" + emailId + "]"; } }
2. Java With Lombok
import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; @NoArgsConstructor @AllArgsConstructor @ToString public class Employee { private @Getter @Setter Integer employeeId; private @Getter @Setter String name; private @Getter @Setter String company; private @Getter @Setter String emailId; }
3. Configuring Lombok To Eclipse
4. Lombok Annotations: @Getter and @Setter
import lombok.Getter; import lombok.Setter; public class Employee { private @Getter @Setter Integer employeeId; private @Getter @Setter String name; private @Getter @Setter String company; private @Getter @Setter String emailId; }
5. Lombok The @NoArgsConstructor Example
import lombok.NoArgsConstructor; @NoArgsConstructor public class Employee { private Integer employeeId; private String name; private String company; private String emailId; }
6. Lombok @AllArgsConstructor Example
import lombok.AllArgsConstructor; @AllArgsConstructor public class Employee { private Integer employeeId; private String name; private String company; private String emailId; }
7. Lombok @ToString Example
import lombok.ToString; @ToString public class Employee { private Integer employeeId; private String name; private String company; private String emailId; }
8. Lombok @EqualsAndHashCode Example
import lombok.EqualsAndHashCode; @EqualsAndHashCode public class Employee { private Integer employeeId; private String name; private String company; private String emailId; }
9. Lombok @Data Example
import lombok.Data; @Data public class Employee { private Integer employeeId; private String name; private String company; private String emailId; }
10. Lombok @Builder Example
import lombok.Builder; @Builder public class Employee { private Integer employeeId; private String name; private String company; private String emailId; }
That's all about Lombok library in Java. It's one of my favorite Java library as it help you to write clean code and improves developer productivity. I highly recommend every Java developer to learn Lombok and there is no better way to learn then using Lombok in your project. You can start using Lombok by following these examples until you get comfortable. All modern IDE like Eclipse and IntelliJIDEA support Lombok, so you can use Lombok classes just like any other regular class in Java.
- 5 Free Courses to learn Object Oriented Programming (courses)
- How to use Factory method design pattern in Java? (tutorial)
- Difference between Factory and Dependency Injection Pattern? (answer)
- 5 Free Courses to learn Data Structure and Algorithms (courses)
- When to use Command Design Pattern in Java (example)
- Difference between Factory and Abstract Factory Pattern? (example)
- 7 Best Courses to learn Design Pattern in Java (courses)
- How to create thread-safe Singleton in Java (example)
- 18 Java Design Pattern Interview Questions with Answers (list)
- How to use Template Design Pattern in Java? (template pattern)
- 7 Best Books to learn the Design Pattern in Java? (books)
- How to implement the Strategy Design Pattern in Java? (example)
- Difference between State and Strategy Design Pattern in Java? (answer)
- 20 System Design Interview Questions (list)
- 10 OOP Design Principle Every Java developer should learn (solid principle)
- How to implement a Decorator design pattern in Java? (tutorial)
- 7 Books to learn System Design for Beginners (System design books)
- How to design a Vending Machine in Java? (questions)
- How to use Composite Design Pattern in Java (composite example)
Thanks a lot for reading this article so far. If you like this example and tutorial of Lombok library in Java then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.