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:

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;
   }

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

 public class Person {
   @Getter @Setter private int age = 10;
   @Setter(AccessLevel.PROTECTED) private String name;
 }

How cool?!?!

Thank you!

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: