Sometimes we need to clone an object in Java, but not a reference (a junior developer may use). There are several ways to achieve this task; however, I recommend to use a copy-constructor (that comes from C++) and, in particular, using @Builder from Lombok (to avoid boilerplate code))
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cl.devweb.constructor; | |
public class Cliente implements Cloneable { | |
int rut; | |
String nombre; | |
Cliente(int rut, String nombre) { | |
this.rut = rut; | |
this.nombre = nombre; | |
} | |
// copy constructor | |
Cliente(Cliente c) { | |
System.out.println("Copy constructor"); | |
rut = c.rut; | |
nombre = c.nombre; | |
} | |
public Object clone() throws CloneNotSupportedException { | |
return super.clone(); | |
} | |
public int getRut() { | |
return rut; | |
} | |
public void setRut(int rut) { | |
this.rut = rut; | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cl.devweb.constructor; | |
public class ClienteClonePoc { | |
public static void main(String[] args) { | |
try { | |
Cliente cliente1 = new Cliente(1, "joe"); | |
Cliente cliente2 = (Cliente) cliente1.clone(); | |
Cliente cliente3 = new Cliente(cliente1); | |
Cliente cliente4 = cliente1; | |
cliente1.setRut(7); | |
cliente1.setNombre("john"); | |
System.out.println("cliente1: " + cliente1.getRut() + " " + cliente1.getNombre()); | |
System.out.println("cliente2: " + cliente2.getRut() + " " + cliente2.getNombre()); | |
System.out.println("cliente3: " + cliente3.getRut() + " " + cliente3.getNombre()); | |
System.out.println("cliente4: " + cliente4.getRut() + " " + cliente4.getNombre()); | |
} catch (CloneNotSupportedException c) { | |
c.printStackTrace(); | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cl.devweb.constructor; | |
public class CopyConstructorPocLombok { | |
public static void main(String[] args) { | |
Premium premium1 = new Premium("pi", 3.14); | |
Premium premium2 = premium1.toBuilder().build(); //copy constructor | |
Premium premium3 = premium1; | |
premium1.setNameOption("e"); | |
premium1.setPremiumValue(2.718); | |
System.out.println("premium1: " + premium1.getNameOption() + " " + premium1.getPremiumValue()); | |
System.out.println("premium2: " + premium2.getNameOption() + " " + premium2.getPremiumValue()); | |
System.out.println("premium3: " + premium3.getNameOption() + " " + premium3.getPremiumValue()); | |
} | |
} |