What's the point of modifying the serialVersionUID?
From what I saw in a course, as long as the serialVersionUID is the same (serialVersionUID = 1L) the program that receives that instance will be able to read it. Now if the recipient of the .dat changes a name property, why is he still able to read the instance without issue?
From what I understand, the serialVersionUID is created to check that the sent instance has what it needs to be readable by the receiving program; now if in this case I change a property, the serialVersion will change and cannot be read.
Now the question, why in the first case does it let me read the file without any problem?
The code:
public class Serializando {
public static void main(String[] args) {
Administrador jefe = new Administrador("Juan",2000,1998,11,24);
Empleado []personal = new Empleado[3];
personal[0] = jefe;
personal[1] = new Empleado("Ana",400,1999,5,29);
personal[2] = new Empleado("Lautaro",500,2012,6,5);
try {
/* ObjectOutputStream escribiendo = new ObjectOutputStream(new FileOutputStream("/Users/MatiEzelQ/Documents/Java SE/Curso/src/AccesoAFicheros/Objeto.dat"));
escribiendo.writeObject(personal);
escribiendo.close();*/
ObjectInputStream leyendo = new ObjectInputStream(new FileInputStream("/Users/MatiEzelQ/Documents/Java SE/Curso/src/AccesoAFicheros/Objeto.dat"));
try {
Empleado []empleadosRecuperados = (Empleado[])leyendo.readObject();
for (int i=0; i<empleadosRecuperados.length;i++) {
System.out.println(empleadosRecuperados[i]);
}
} catch (ClassNotFoundException e) {}
leyendo.close();
} catch (IOException e) {
System.out.println("Error");
}
}
}
class Empleado implements Serializable {
private static final long serialVersionUID = 2L;
private String nombre;
private double sueldo;
Date fechaContrato;
public Empleado(String n, double s, int agno, int mes, int dia) {
nombre = n;
sueldo = s;
GregorianCalendar calendario = new GregorianCalendar(agno,mes-1,dia);
fechaContrato = calendario.getTime();
}
public void subirSueldo(int porcentaje) {
sueldo += sueldo * porcentaje / 100;
}
public String toString() {
return "Nombre: " + nombre + ". Sueldo: " + sueldo + ". Fecha de contrato: " + fechaContrato;
}
//Getters-Setters
public Date getFechaContrato() {
return fechaContrato;
}
public void setFechaContrato(Date fechaContrato) {
this.fechaContrato = fechaContrato;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public double getSueldo() {
return sueldo;
}
public void setSueldo(double sueldo) {
this.sueldo = sueldo;
}
}
class Administrador extends Empleado {
private static final long serialVersionUID = 2L;
public Administrador(String n, double s, int agno, int mes, int dia) {
super(n, s, agno, mes, dia);
incentivo = 0;
}
public double getSueldo() {
return super.getSueldo() + incentivo;
}
private int incentivo;
}
The field
static final long serialVersionUID
is declared for classes that implement the interfaceSerializable
. The class documentation explains what this field is and what it is used for. Here I place the main texts of the documentation to use in the answer:Translated:
As the documentation indicates, the fact of having the ability to declare the field
serialVersionUID
is to maintain a standard value when serializing and deserializing the objects of a class. This is mainly the case when you have your objects serialized in a file (in this case you mention your .dat) and you have serialized it using a class compiled on Windows Vista 32bit with JDK 1.6 u32 and this file you read from an Ubuntu 14 computer 64-bit with JDK 1.8 u45 where the same class has been recompiled. Then, since there is an operating system difference, Java can use the value you have declared in the class for serialVersionUID and make it easy to check that the object to be deserialized is compatible with the class loaded at program execution.What can you modify it for? Because then your application can immediately fail when trying to deserialize an object whose version is not the latest of the class to which it corresponds. This is known as fail fast (I didn't find a corresponding article in Spanish)
It is highly recommended that you declare this field and modify it only if you have modified the field structure of the class. Also, if you can't think of what value to assign to it, you can leave that job to the IDE, I've seen Eclipse and IntelliJ Idea do it very well.