Wednesday, February 8, 2012

Understanding serialVersionUID

If we ever wonder why we need to declare a serialversionUID when we implement java.util.Serializable, this article can provide a good idea why not declaring a serialVersionUID can cause a problem.
import java.io.Serializable;

public class Hello implements Serializable {
//    private static final long serialVersionUID = 1L;
    private String message;

//    public String getTheSameMessage() {
//        return message;
//    }
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Hello [message=").append(message).append("]");
        return builder.toString();
    }
}
package org.fredy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Main {
    public static void main(String[] args) throws Exception {
        Hello h = new Hello();
        h.setMessage("Hello World");
        
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(
                new FileOutputStream(new File("out.obj")));
            oos.writeObject(h);
        } finally{
            if (oos != null) {
                oos.close();
            }
        }
        
//        ObjectInputStream ois = null;
//        try {
//            ois = new ObjectInputStream(
//                new FileInputStream(new File("out.obj")));
//            Hello h1 = (Hello) ois.readObject();
//            System.out.println(h1.getMessage());
//        }
//        finally {
//            if (ois != null) {
//                ois.close();
//            }
//        }
    }
}
Now execute the Main class. This will create a file named out.obj.

Let's now uncomment some lines in Hello class and comment out some lines in Main class as depicted below.

import java.io.Serializable;

public class Hello implements Serializable {
//    private static final long serialVersionUID = 1L;
    private String message;

    public String getTheSameMessage() {
        return message;
    }
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Hello [message=").append(message).append("]");
        return builder.toString();
    }
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Main {
    public static void main(String[] args) throws Exception {
//        Hello h = new Hello();
//        h.setMessage("Hello World");
//        
//        ObjectOutputStream oos = null;
//        try {
//            oos = new ObjectOutputStream(
//                new FileOutputStream(new File("out.obj")));
//            oos.writeObject(h);
//        } finally{
//            if (oos != null) {
//                oos.close();
//            }
//        }
        
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(
                new FileInputStream(new File("out.obj")));
            Hello h1 = (Hello) ois.readObject();
            System.out.println(h1.getMessage());
        }
        finally {
            if (ois != null) {
                ois.close();
            }
        }
    }
}
Execute the Main class again. A java.io.InvalidClassException will be thrown because the Hello class that was serialized is no longer compatible with updated Hello class. To fix this problem, we need to declare serialVersionUID in the Hello class.

No comments:

Post a Comment