In this tutorial, I will show you how we can use Project Lombok to generate Getter, Setter methods or the constructors in a Java object automatically.
OK, first of all, we need to create an example Maven project.
with Project Lombok dependency as below:
1 2 3 4 5 6 |
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> <scope>provided</scope> </dependency> |
Remember that, we need to install Project Lombok plugin into our IDE, so that the IDE can understand and no compile errors. See construction for IntelliJ IDEA at here.
Now, for example, I have a Student class with some information like name, code, dateOfBirth. Without using Project Lombok library, I must declare Student class as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.huongdanjava.lombok; import java.util.Date; public class Student { private String name; private String code; private Date dateOfBirth; public Student(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public Date getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(Date dateOfBirth) { this.dateOfBirth = dateOfBirth; } } |
With Project Lombok, I can remove Getter, Setter methods in Student class and just for Project Lombok generate them by using @Getter, @Setter annotation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.huongdanjava.lombok; import lombok.Getter; import lombok.Setter; import java.util.Date; @Getter @Setter public class Student { private String name; private String code; private Date dateOfBirth; public Student(String name) { this.name = name; } } |
and add a default constructor, a constructor with all properties into Student class by using @NoArgsConstructor and @AllArgsConstructor annotation as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.huongdanjava.lombok; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import java.util.Date; @Getter @Setter @NoArgsConstructor @AllArgsConstructor public class Student { private String name; private String code; private Date dateOfBirth; public Student(String name) { this.name = name; } } |
Now, you can use our Student class with Getter, Setter, Constructors which we already declared with Project Lombok.
For example:
1 2 3 4 5 6 7 8 9 10 11 |
package com.huongdanjava.lombok; public class Example { public static void main(String[] args) { Student student = new Student(null, null, null); student.setName("Khanh"); System.out.println(student.getName()); } } |
Result:
We can declare Getter, Setter only for some properties by using @Getter, @Setter annotation for that properties. For example, I only need Getter, Setter method for property code in Student class, I will declare as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.huongdanjava.lombok; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import java.util.Date; @NoArgsConstructor @AllArgsConstructor public class Student { private String name; @Getter @Setter private String code; private Date dateOfBirth; public Student(String name) { this.name = name; } } |
Now, you can only Getter, Setter for property code for Student object like below: