Project Lombok is a Java library that uses annotations to generate templated code during compilation, such as Getters, Setters, and constructors. Project Lombok annotations like @Getter, @Setter, @NoArgsConstructor, @AllArgsConstructor, @Data, etc., allow us to minimize the need to manually write repetitive code. In this tutorial, I will show you how to use Project Lombok to automatically generate Setter, Getter methods and constructors in a Java class!
First of all, I will 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.48</version> <scope>provided</scope> </dependency> |
You also need to configure the annotation processor in the Maven Compiler Plugin as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.48</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build> |
so when you compile the code with Maven, the code related to Lombok will be processed.
For the IDE you’re working with, you also need to install the Project Lombok plugin so that the IDE can understand it and avoid compilation errors. You can find instructions on installing the Project Lombok plugin in IntelliJ IDE 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 and Setter methods in Student class and just for Project Lombok to generate them by using @Getter, @Setter annotations:
|
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; } } |
For constructors, you can add a constructor with no parameters and a constructor with all the information of the Student class by using the annotations @NoArgsConstructor and @AllArgsConstructor as follows:
|
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; } } |
If your class defines some required fields (final fields or fields defined with Lombok’s @NonNull annotation), you can also use the @RequiredArgsConstructor annotation to generate constructors for these required fields, as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.huongdanjava.lombok; import java.time.LocalDate; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.Setter; @Getter @Setter @NoArgsConstructor @AllArgsConstructor @RequiredArgsConstructor public class Student { private String name; @NonNull private String code; private LocalDate dateOfBirth; } |
When Maven or your IDE compiles the Student class, you will see that the content of the Student class includes Getters, Setters, and a constructor as follows:
|
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.huongdanjava.lombok; import java.time.LocalDate; import lombok.Generated; import lombok.NonNull; public class Student { private String name; private @NonNull String code; private LocalDate dateOfBirth; @Generated public String getName() { return this.name; } @Generated public @NonNull String getCode() { return this.code; } @Generated public LocalDate getDateOfBirth() { return this.dateOfBirth; } @Generated public void setName(String name) { this.name = name; } @Generated public void setCode(@NonNull String code) { if (code == null) { throw new NullPointerException("code is marked non-null but is null"); } else { this.code = code; } } @Generated public void setDateOfBirth(LocalDate dateOfBirth) { this.dateOfBirth = dateOfBirth; } @Generated public Student() { } @Generated public Student(String name, @NonNull String code, LocalDate dateOfBirth) { if (code == null) { throw new NullPointerException("code is marked non-null but is null"); } else { this.name = name; this.code = code; this.dateOfBirth = dateOfBirth; } } @Generated public Student(@NonNull String code) { if (code == null) { throw new NullPointerException("code is marked non-null but is null"); } else { this.code = code; } } } |
All the Getters, Setters, and constructors have been fully generated.
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; import java.time.LocalDate; public class Example { static void main(String[] args) { Student student = new Student("Khanh", "123", LocalDate.now()); System.out.println(student.getName()); } } |
Result:

If the @Getter and @Setter annotations are declared at the class level, Project Lombok will generate Getter and Setter for all non-static fields of that class. You can also declare Getter and Setter for only certain fields of a Java class using the @Getter and @Setter annotations. For example, if I only want to declare Getter and Setter for the code field of the Student class, I would declare it as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.huongdanjava.lombok; import java.time.LocalDate; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.Setter; @NoArgsConstructor @AllArgsConstructor @RequiredArgsConstructor public class Student { private String name; @Getter @Setter @NonNull private String code; private LocalDate dateOfBirth; } |
Now, you can only use Getter and Setter for the code field of the Student class:

You can also define access modifiers for these Getter and Setter methods and constructors using the AccessLevel class, for example:
|
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 |
package com.huongdanjava.lombok; import java.time.LocalDate; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.Setter; @Getter @Setter @NoArgsConstructor(access = AccessLevel.PRIVATE) @AllArgsConstructor @RequiredArgsConstructor public class Student { private String name; @Setter(AccessLevel.PRIVATE) @NonNull private String code; @Getter(AccessLevel.PROTECTED) private LocalDate dateOfBirth; } |
At this point, Maven or the IDE will generate the Student class during compile time as follows:
|
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.huongdanjava.lombok; import java.time.LocalDate; import lombok.Generated; import lombok.NonNull; public class Student { private String name; private @NonNull String code; private LocalDate dateOfBirth; @Generated public String getName() { return this.name; } @Generated public @NonNull String getCode() { return this.code; } @Generated public void setName(String name) { this.name = name; } @Generated public void setDateOfBirth(LocalDate dateOfBirth) { this.dateOfBirth = dateOfBirth; } @Generated private Student() { } @Generated public Student(String name, @NonNull String code, LocalDate dateOfBirth) { if (code == null) { throw new NullPointerException("code is marked non-null but is null"); } else { this.name = name; this.code = code; this.dateOfBirth = dateOfBirth; } } @Generated public Student(@NonNull String code) { if (code == null) { throw new NullPointerException("code is marked non-null but is null"); } else { this.code = code; } } @Generated private void setCode(@NonNull String code) { if (code == null) { throw new NullPointerException("code is marked non-null but is null"); } else { this.code = code; } } @Generated protected LocalDate getDateOfBirth() { return this.dateOfBirth; } } |
As you can see, the constructor with no parameters is generated with the access modifier private. The Setter method for the code field is generated with the access modifier private. And the Setter method for the dateOfBirth field has been generated with the access modifier protected.