Core Java - Serialization


Java supports object serialisation, which converts an object into a sequence of bytes including its contents, type, and data types.

After writing a serialised object to a file, it can be read and deserialised. This means that the type information and bytes can be used to rebuild the object in memory. The process is JVM agnostic, allowing for object serialisation and deserialisation across multiple platforms.

The ObjectInputStream and ObjectOutputStream classes provide methods for serialising and deserialising objects.
The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out:

    public final void writeObject(Object x)throws IOException

The method shown above serialises an object and sends it to the output stream. Similarly, the ObjectInputStream class provides a method for deserialising an object:

    public final Object readObject()throws IOException, ClassNotFoundException

This method extracts the next Object from the stream and deserialises it. To use the return value, cast it to the proper data type.

To explain serialisation in Java, I will utilise the Employee class, which we explored earlier in the book. Assume we have the following Employee class that implements the Serializable interface.

    import java.io.*;
    public class Employee implements java.io.Serializable{
        public String name;
        public String address;
        public transient int SSN;
        public int number;
        public void mailCheck(){
        System.out.println("Mailing a check to "+ name+" "+ address);
        }
    }

To successfully serialise a class, two conditions must be met:
  • The class must implement the java.io.Serializable interface.
  • All fields in the class must be serializable. If a field is not serializable, it must be tagged as temporary.

  • If you want to know whether or not a Java Standard Class is serializable, look at its documentation. The test is simple. If the class supports java.io.If anything is serializable, it is; otherwise, it is not.

    Serializing an Object :
    The ObjectOutputStream class is used to serialize an Object. The following SerializeDemo program instantiates an Employee object and serializes it to a file.
    When the program is done executing, a file named employee.ser is created. The program does not generate any output, but study the code and try to determine what the program is doing.
    Note : When serializing an object to a file, the standard convention in Java is to give the file a .serextension.
        import java.io.*;
        public class SerializeDemo{
            public static void main(String[] args){
            Employee e =new Employee();
            e.name ="Virat";
            e.address ="India";
            e.SSN =155;
            e.number =18;
            try{
            FileOutputStream fileOut =new FileOutputStream("employee.ser");
            ObjectOutputStream out=new ObjectOutputStream(fileOut);
            out.writeObject(e);
            out.close();
            fileOut.close();
            }catch(IOException i){
            i.printStackTrace();
            }
            }
        }
    
    
    Deserializing an Object :
    Observe the following To deserialise the Employee object created in the SerializeDemo programme, run the DeserializeDemo programme.
    Examine the programme and try to predict its output:
    
        import java.io.*;
        public class DeserializeDemo{
            public static void main(String[] args){
            Employee e =null;
            try{
            FileInputStream fileIn =new FileInputStream("employee.ser");
            ObjectInputStream in=new ObjectInputStream(fileIn);
            e =(Employee)in.readObject();
            in.close();
            fileIn.close();
            }catch(IOException i){
            i.printStackTrace();
            return;
            }catch(ClassNotFoundException c){
            System.out.println("Employee class not found");
            c.printStackTrace();
            return;
            }
            System.out.println("Deserialized Employee...");
            System.out.println("Name: "+ e.name);
            System.out.println("Address: "+ e.address);
            System.out.println("SSN: "+ e.SSN);
            System.out.println("Number: "+ e.number);
            }
        }
    
    
    Output :
    Deserialized Employee...
    Name: Virat
    Address: India
    SSN: 0
    Number: 18

    Here are following important points to be noted:
  • The readObject() method declares a ClassNotFoundException, which is attempted to be caught by the try/catch block. A JVM has to be able to locate the class's bytecode in order to deserialise an object. During object deserialisation, the JVM throws a ClassNotFoundException if it cannot locate a class.
  • You'll see that readObject() returns a value that is cast to an Employee reference.
  • When the object was serialised, the value of the SSN field was 11122333; however, since the field is temporary, this value was not delivered to the output stream. The deserialised Employee object has an SSN field of 0.


  • (Core Java - Networking)