I serialse a student class with a private static final long serialVersionUID = 1L and deserialise it.When I change of the instance variable of the Student class and deserialse it,say suppose I changed the name instance variable to fullName and call deserialse without serialising this modifed Student class. I am still able to do the below cast in deserialse method
Student stud=(Student) ois.readObject();
Why doesnt it throw any exception during casting as the incoming deserilased class and the new modifed class are diffrent.Also all the instance variable which remained unmodifed has value restored back after deserialization except the modified ones. However I get the java.io.InvalidClassException when I remove the private static final long serialVersionUID = 1L from my student class and deserialse which is understandable. So When the serialVersionUID match, doesnt it check for any other attributes of the class? What is the concept behind this?
class Student implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int id;
public String name() {
return name;
}
public void setname(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}