Specifying an Explicit Version
Home ] Up ] A Simple Application ] Changing a Class ] [ Specifying an Explicit Version ]

 

 

But we'd sometimes like to change the class in a way that should not disable the ability to serialize older versions in.

In order to do this, we have to specify the version number explicitly. 

You do this by specifying a field in your class:

static final long serialVersionUID = 8364956740597978489L;

The long numeric literal is the version number. Where did it come from?

The JDK supplies a program called serialver which can generate a field declaration similar to the above. Once we've used serialver to determine the version number for the original class, we can add this line to the modified class and try it again.

Voila! The serialization in now works!

Now we can augment our serialization programs to support the phone number:

package serialization;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializeOutAddressBook
{
    public static void main(String[] args)
    {
        AddressBook book = new AddressBook();
        book.add("Bill Gates", "1000 High-on-the-Hog Drive",
                                "981-786-5421");
        book.add("Scott McNealy", "4000 Java Circle");
        book.add("Linus Torvalds", "300 Linux Lane",
                                "653-876-2378");
        
        FileOutputStream fout = null;
        ObjectOutputStream out = null;
        try
        {
            fout = new FileOutputStream("addrbook2.ser");
            out = new ObjectOutputStream(fout);
            out.writeObject(book);
            
            System.out.println("Serialization successful!");
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                if (out != null)
                    out.close();
                if (fout != null)
                    fout.close();
            }
            catch(IOException ex)
            {   /* Do nothing */ }
        }
    }
}
package serialization;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Enumeration;

public class SerializeInAddressBook
{
    public static void main(String[] args)
    {
        AddressBook book = null;
        FileInputStream fin = null;
        ObjectInputStream in = null;
        try
        {
            Enumeration en = null;
            
            System.out.println("Serializing in addrbook2.ser...");
            
            fin = new FileInputStream("addrbook2.ser");
            in = new ObjectInputStream(fin);
            book = (AddressBook) in.readObject();
            
            System.out.println(
                    "Serialization of addrbook2.ser successful!");
        
            en = book.addresses();
            while (en.hasMoreElements())
            {
                Address addr = (Address) en.nextElement();
                System.out.println(
                    "Name: [" + addr.getName() + "], " +
                    "Address: [" + addr.getAddress() + "]" +
                    "Phone: [" + addr.getPhone() + "]");
            }
            
            in.close();
            
            System.out.println("Serializing in addrbook.ser...");
            
            fin = new FileInputStream("addrbook.ser");
            in = new ObjectInputStream(fin);
            book = (AddressBook) in.readObject();
            
            System.out.println(
                    "Serialization of addrbook.ser successful!");
            
            en = book.addresses();
            while (en.hasMoreElements())
            {
                Address addr = (Address) en.nextElement();
                System.out.println(
                    "Name: [" + addr.getName() + "], " +
                    "Address: [" + addr.getAddress() + "]" +
                    "Phone: [" + addr.getPhone() + "]");
            }
            
        }
        catch(ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                if (in != null)
                    in.close();
                if (fin != null)
                    fin.close();
            }
            catch(IOException ex)
            {   /* Do nothing */ }
        }
    }
}

This now works correctly.

 
The page was last updated February 19, 2008