Java Getters and Setters with Lombok

Sick of all the boilerplate code required to create simple getter/setters with Java?
Checkout project Lombok!

Let’s suppose we want to create a simple class with the following:

  • public getter/setter for age
  • protected setter for name

With Vanilla Java, we might do the following:

[code language=”java”]
public class Person {
private int age = 10;
private String name;

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;<
}
protected void setName(String name) {
this.name = name;
}
[/code]

However, with Lombok (we remove most of the cruft) and we simply write:

[code language=”Java”]
public class Person {
@Getter @Setter private int age = 10;
@Setter(AccessLevel.PROTECTED) private String name;
}
[/code]

How cool?!?!

Thank you!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *