wither vs builder Lombok library

user1692342 picture user1692342 · Mar 17, 2017 · Viewed 7k times · Source

I have started using the Lombok library, and I am unable to figure out the difference between using a wither & a builder.

@Builder
@Wither
public class Sample {
   private int x;
   private int y;
}

Now I can create an object in 2 ways:

Sample s = new Sample().builder()
              .x(10)
              .y(15)
              .build();

OR

Sample s = new Sample()
           .withx(10)
           .withy(10);

What is the difference between the two? Which one should I use?

Answer

Roel Spilker picture Roel Spilker · Mar 20, 2017

@Builder is used to create mutable objects, @Wither for immutables.

Disclosure: I am a lombok developer.