Posts

Showing posts from April, 2011

What is serialVersionUID?

If a class implements Serializable interface, your IDE worn you to add serialVersionUID or it add automatically. This variable holds a private static final long and IDE automatically declared it to 1L. class SerializeMe implements Serializable { private static final long serialVersionUID = 1L; private String data; public SerializeMe (String data) { this.data = data; } public String getData() { return data; } } Field data represents some information stored in the class. Note that serialVersionUID is a static value, then it should not have been serialized. But why is this so impotent? public class SerialVersionUIDTester { public static void main(String [] args) throws Exception { File file = new File("out.ser"); FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); SerializeMe serializeMe = new SerializeMe("serialVersionUID is 1L");