Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

270
Vistas
wither vs builder Lombok library

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?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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

Disclosure: I am a lombok developer.

about 4 years ago · Santiago Trujillo Denunciar

0

Generally, the difference is when you build a object with builder(), you must call build() method at last, and before you call build(), all property values are saved in the internal builder object instead of the object your created with new. After you setted all properties and call build(), a new object will be created. See details here: https://projectlombok.org/features/Builder.html . I think the better way for builder pattern is:

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

Because the first Sample object is redundant.

For withers, every time you called withXXX(xxx), a new object is returned, with XXX set to xxx, and all other properties cloned from the object you called wither on (if xxx is different from the original xxx. See details here: https://projectlombok.org/features/experimental/Wither.html). Choose which way, I think it's only depends on your personal habit and your project's code style.

Hope this could help you.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda