Coding Exercise: Demonstrate Lombok in Java
Lombok is a popular library in Java that helps reduce boilerplate code. One of its most used features is to generate getter, setter, constructor, and toString()
methods without having to manually write them. Let’s use that as a basis for our exercise.
Requirements
- Setup a new Java project with Maven.
- Add Lombok as a dependency.
- Create a simple
Person
class with a few fields. - Use Lombok annotations to auto-generate the required methods.
Steps
- Setup Lombok Dependency
For Maven, add the following to your pom.xml
:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version> <!-- You can check for the latest version on Maven Central -->
<scope>provided</scope>
</dependency>
- Create the Person class
import lombok.Getter;
import lombok.Setter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.AllArgsConstructor;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Person {
private String firstName;
private String lastName;
private int age;
}
- Test the Person class
Now create a Main
class to test out the Person
class:
public class Main {
public static void main(String[] args) {
Person person = new Person("John", "Doe", 25);
System.out.println(person); // This should print something like "Person(firstName=John, lastName=Doe, age=25)"
person.setAge(26);
System.out.println(person.getAge()); // This should print "26"
}
}
Expected Output
Person(firstName=John, lastName=Doe, age=25)
26
Reading
Go to the Lombok homepage and read the documentation. https://projectlombok.org/ or to our Toolbox for more information.
Challenge
For those who want to explore further, try adding more Lombok annotations to the Person
class like @EqualsAndHashCode
, and @Builder
and observe the functionalities they bring.