Home » Java » What is object serialization? [duplicate]

What is object serialization? [duplicate]

Posted by: admin November 2, 2017 Leave a comment


This question already has an answer here:


Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialized – converted into a replica of the original object.


You can think of serialization as the process of converting an object instance into a sequence of bytes (which may be binary or not depending on the implementation).

It is very useful when you want to transmit one object data across the network, for instance from one JVM to another.

In Java, the serialization mechanism is built into the platform, but you need to implement the Serializable interface to make an object serializable.

You can also prevent some data in your object from being serialized by marking the attribute as transient.

Finally you can override the default mechanism, and provide your own; this may be suitable in some special cases. To do this, you use one of the hidden features in java.

It is important to notice that what gets serialized is the “value” of the object, or the contents, and not the class definition. Thus methods are not serialized.

Here is a very basic sample with comments to facilitate its reading:

import java.io.*;
import java.util.*;

// This class implements "Serializable" to let the system know
// it's ok to do it. You as programmer are aware of that.
public class SerializationSample implements Serializable {

    // These attributes conform the "value" of the object.

    // These two will be serialized;
    private String aString = "The value of that string";
    private int    someInteger = 0;

    // But this won't since it is marked as transient.
    private transient List<File> unInterestingLongLongList;

    // Main method to test.
    public static void main( String [] args ) throws IOException  { 

        // Create a sample object, that contains the default values.
        SerializationSample instance = new SerializationSample();

        // The "ObjectOutputStream" class has the default 
        // definition to serialize an object.
        ObjectOutputStream oos = new ObjectOutputStream( 
                               // By using "FileOutputStream" we will 
                               // Write it to a File in the file system
                               // It could have been a Socket to another 
                               // machine, a database, an in memory array, etc.
                               new FileOutputStream(new File("o.ser")));

        // do the magic  
        oos.writeObject( instance );
        // close the writing.

When we run this program, the file “o.ser” is created and we can see what happened behind.

If we change the value of: someInteger to, for example Integer.MAX_VALUE, we may compare the output to see what the difference is.

Here’s a screenshot showing precisely that difference:

alt text

Can you spot the differences? 😉

There is an additional relevant field in Java serialization: The serialversionUID but I guess this is already too long to cover it.


Daring to answer 6 year old question, adding just a very high level understanding for people new to Java

What is Serialization?

Converting an object to bytes and bytes back to object (Deserialization).

when is serialization used?

When we want to Persist the Object.
When we want the object to exist beyond the lifetime of the JVM.

Real World Example:

ATM: When the account holder tries to withdraw money from the server through ATM, the account holder information like withdrawl details will be serialized and sent to server where the details are deserialized and used to perform operations.

How serialization is performed in java.

  1. Implement java.io.Serializable interface (marker interface so no method to implement).

  2. Persist the object: Use java.io.ObjectOutputStream class, a filter stream which is a wrapper around a lower-level byte stream (to write Object to file systems or transfer a flattened object across a network wire and rebuilt on the other side).

    • writeObject(<<instance>>) – to write an object
    • readObject() – to read an serialized Object


When you serialize an object, only the object’s state will be saved, not the object’s class file or methods.

When you serialized a 2 byte object, you see 51 bytes serialized file.

Steps how the object is serialized and de-serialized.

Answer for: How did it convert to 51 bytes file?

  • First writes the serialization stream magic data (STREAM_MAGIC= “AC ED” and STREAM_VERSION=version of the JVM).
  • Then it writes out the metadata of the class associated with an instance (length of the class, the name of the class, serialVersionUID).
  • Then it recursively writes out the metadata of the superclass until it finds java.lang.Object.
  • Then starts with the actual data associated with the instance.
  • Finally writes the data of objects associated with the instance starting from metadata to actual content.

If you are interested in more in dept information about Java Serialization please check this link.

Edit : One more good link to read.

This will answer a few frequent questions:

  1. How not to serialize any field in class.
    Ans: use transient keyword

  2. When child class is serialized does parent class get serialized?
    Ans: No, If parent is not extending Serializable interface parents field don’t get serialized.

  3. When parent is serialized does child class get serialized?
    Ans: Yes, by default child class also get serialized.

  4. How to avoid child class from getting serialized?
    Ans: a. Override writeObject and readObject method and throw NotSerializableException.

    b. also you can mark all fields transient in child class.

  5. Some system-level classes such as Thread, OutputStream and its subclasses, and Socket are not serializable.

Serialization is taking a “live” object in memory and converting it to a format that can be stored somewhere (eg. in memory, on disk) and later “deserialized” back into a live object.


I liked the way @OscarRyz presents. Although here i am continuing the story of serialization which was originally written by @amitgupta.

Even though knowing about the robot class structure and having serialized data Earth’s scientist were not able to deserialize the data which can make robots working.

Exception in thread "main" java.io.InvalidClassException:
SerializeMe; local class incompatible: stream classdesc

Mars’s scientists were waiting for the complete payment. Once the payment was done Mars’s scientists shared the serialversionUID with Earth’s scientists. Earth’s scientist set it to robot class and everything became fine.


Serialization means persisting objects in java. If you want to save the state of the object and want to rebuild the state later (may be in another JVM) serialization can be used.

Note that the properties of an object is only going to be saved. If you want to resurrect the object again you should have the class file, because the member variables only will be stored and not the member functions.


ObjectInputStream oos = new ObjectInputStream(                                 
                                 new FileInputStream(  new File("o.ser")) ) ;
SerializationSample SS = (SearializationSample) oos.readObject();

The Searializable is a marker interface which marks that your class is serializable. Marker interface means that it is just an empty interface and using that interface will notify the JVM that this class can be made serializable.


Serialization is the process of converting an object’s state to bits so that it can be stored on a hard drive. When you deserialize the same object, it will retain its state later. It lets you recreate objects without having to save the objects’ properties by hand.



My Two cents from my own blog:

Here is a detailed explanation of the Serialization: (my own blog)


Serialization is the process of persisting the state of an object. It is represented and stored in the form of a sequence of bytes. This can be stored in a file. The process to read the state of the object from the file and restoring it is called deserialization.

What is the need of Serialization?

In modern day architecture, there is always a need to store object state and then retrieve it. For example in Hibernate, to store a object we should make the class Serializable. What it does, is that once the object state is saved in the form of bytes it can be transferred to another system which can then read from the state and retrieve the class. The object state can come from a database or a different jvm or from a separate component. With the help of Serialization we can retrieve the Object state.

Code Example and explanation:

First let’s have a look at the Item Class:

public class Item implements Serializable{

    *  This is the Serializable class
    private static final long serialVersionUID = 475918891428093041L;
    private Long itemId;
    private String itemName;
    private transient Double itemCostPrice;
    public Item(Long itemId, String itemName, Double itemCostPrice) {
        this.itemId = itemId;
        this.itemName = itemName;
        this.itemCostPrice = itemCostPrice;

      public Long getItemId() {
          return itemId;

      public String toString() {
          return "Item [itemId=" + itemId + ", itemName=" + itemName + ", itemCostPrice=" + itemCostPrice + "]";

       public void setItemId(Long itemId) {
           this.itemId = itemId;

       public String getItemName() {
           return itemName;
       public void setItemName(String itemName) {
            this.itemName = itemName;

       public Double getItemCostPrice() {
            return itemCostPrice;

        public void setItemCostPrice(Double itemCostPrice) {
             this.itemCostPrice = itemCostPrice;

In the above code it can be seen that Item class implements Serializable.

This is the interface that enables a class to be serializable.

Now we can see a variable called serialVersionUID is initialized to Long variable. This number is calculated by the compiler based on the state of the class and the class attributes. This is the number that will help the jvm identify the state of an object when it reads the state of the object from file.

For that we can have a look at the official Oracle Documentation:

The serialization runtime associates with each serializable class a
version number, called a serialVersionUID, which is used during
deserialization to verify that the sender and receiver of a serialized
object have loaded classes for that object that are compatible with
respect to serialization. If the receiver has loaded a class for the
object that has a different serialVersionUID than that of the
corresponding sender’s class, then deserialization will result in an
InvalidClassException. A serializable class can declare its own
serialVersionUID explicitly by declaring a field named
“serialVersionUID” that must be static, final, and of type long:
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L; If a
serializable class does not explicitly declare a serialVersionUID,
then the serialization runtime will calculate a default
serialVersionUID value for that class based on various aspects of the
class, as described in the Java(TM) Object Serialization
Specification. However, it is strongly recommended that all
serializable classes explicitly declare serialVersionUID values, since
the default serialVersionUID computation is highly sensitive to class
details that may vary depending on compiler implementations, and can
thus result in unexpected InvalidClassExceptions during
deserialization. Therefore, to guarantee a consistent serialVersionUID
value across different java compiler implementations, a serializable
class must declare an explicit serialVersionUID value. It is also
strongly advised that explicit serialVersionUID declarations use the
private modifier where possible, since such declarations apply only to
the immediately declaring class–serialVersionUID fields are not
useful as inherited members.

If you have noticed there is another keyword we have used which is transient.

If a field is not serializable, it must be marked transient. Here we marked the itemCostPrice as transient and don’t want it to be written in a file

Now let’s have a look on how to write the state of an object in the file and then read it from there.

public class SerializationExample {

    public static void main(String[] args){

    public static void serialize(){

         Item item = new Item(1L,"Pen", 12.55);
         System.out.println("Before Serialization" + item);

         FileOutputStream fileOut;
         try {
             fileOut = new FileOutputStream("/tmp/item.ser");
             ObjectOutputStream out = new ObjectOutputStream(fileOut);
             System.out.println("Serialized data is saved in /tmp/item.ser");
           } catch (FileNotFoundException e) {

           } catch (IOException e) {


    public static void deserialize(){
        Item item;

        try {
                FileInputStream fileIn = new FileInputStream("/tmp/item.ser");
                ObjectInputStream in = new ObjectInputStream(fileIn);
                item = (Item) in.readObject();
                System.out.println("Serialized data is read from /tmp/item.ser");
                System.out.println("After Deserialization" + item);
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } catch (ClassNotFoundException e) {

In the above we can see an example of serialization and deserialization of an object.

For that we used two classes. For serializing the object we have used ObjectOutputStream. We have used the method writeObject to write the object in the file.

For Deserializing we have used ObjectInputStream which reads from the object from the file. It uses readObject to read the object data from the file.

The output of the above code would be like:

Before SerializationItem [itemId=1, itemName=Pen, itemCostPrice=12.55]
Serialized data is saved in /tmp/item.ser
After DeserializationItem [itemId=1, itemName=Pen, itemCostPrice=null]

Notice that itemCostPrice from deserialized object is null as it was not written.

We have already discussed the basics of Java Serialization in part I of this article.

Now let’s discuss it deeply and how it works.

First let’s start with the serialversionuid.

The serialVersionUID is used as a version control in a Serializable class.

If you do not explicitly declare a serialVersionUID, JVM will do it for you automatically, based on various properties of the Serializable class.

Java’s Algorithm of Calculating serialversionuid (Read more details here)

  1. The class name.
    1. The class modifiers written as a 32-bit integer.
    2. The name of each interface sorted by name.
    3. For each field of the class sorted by field name (except private static and private transient fields: The name of the field. The
      modifiers of the field written as a 32-bit integer. The descriptor
      of the field.
    4. If a class initializer exists, write out the following: The name of the method, .
    5. The modifier of the method, java.lang.reflect.Modifier.STATIC, written as a 32-bit integer.
    6. The descriptor of the method, ()V.
    7. For each non-private constructor sorted by method name and signature: The name of the method, . The modifiers of the
      method written as a 32-bit integer. The descriptor of the method.
    8. For each non-private method sorted by method name and signature: The name of the method. The modifiers of the method written as a
      32-bit integer. The descriptor of the method.
    9. The SHA-1 algorithm is executed on the stream of bytes produced by DataOutputStream and produces five 32-bit values sha[0..4]. The
      hash value is assembled from the first and second 32-bit values of the
      SHA-1 message digest. If the result of the message digest, the five
      32-bit words H0 H1 H2 H3 H4, is in an array of five int values named
      sha, the hash value would be computed as follows:
    long hash = ((sha[0] >>> 24) & 0xFF) |
>            ((sha[0] >>> 16) & 0xFF) << 8 |
>            ((sha[0] >>> 8) & 0xFF) << 16 |
>            ((sha[0] >>> 0) & 0xFF) << 24 |
>            ((sha[1] >>> 24) & 0xFF) << 32 |
>            ((sha[1] >>> 16) & 0xFF) << 40 |
>            ((sha[1] >>> 8) & 0xFF) << 48 |
>        ((sha[1] >>> 0) & 0xFF) << 56;

Java’s serialization algorithm

The algorithm to serialize an object is described as below:
1. It writes out the metadata of the class associated with an instance.
2. It recursively writes out the description of the superclass until it finds java.lang.object.
3. Once it finishes writing the metadata information, it then starts with the actual data associated with the instance. But this time, it
starts from the topmost superclass.
4. It recursively writes the data associated with the instance, starting from the least superclass to the most-derived class.

Things To Keep In Mind:

  1. Static fields in a class cannot be serialized.

    public class A implements Serializable{
         String s;
         static String staticString = "I won't be serializable";
  2. If the serialversionuid is different in the read class it will throw a InvalidClassException exception.

  3. If a class implements serializable then all its sub classes will also be serializable.

    public class A implements Serializable {....};
    public class B extends A{...} //also Serializable
  4. If a class has a reference of another class, all the references must be Serializable otherwise serialization process will not be performed. In such case, NotSerializableException is thrown at runtime.


public class B{
     String s,
     A a; // class A needs to be serializable i.e. it must implement Serializable


Serialization is the process of saving an object in a storage medium (such as a file, or a memory buffer) or to transmit it over a network connection in binary form. The serialized objects are JVM independent and can be re-serialized by any JVM. In this case the “in memory” java objects state are converted into a byte stream. This type of the file can not be understood by the user. It is a special types of object i.e. reused by the JVM (Java Virtual Machine). This process of serializing an object is also called deflating or marshalling an object.

The object to be serialized must implement java.io.Serializable Interface.
Default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields.

class ObjectOutputStream extends java.io.OutputStream implements ObjectOutput,

ObjectOutput interface extends the DataOutput interface and adds methods for serializing objects and writing bytes to the file. The ObjectOutputStream extends java.io.OutputStream and implements ObjectOutput interface. It serializes objects, arrays, and other values to a stream. Thus the constructor of ObjectOutputStream is written as:

ObjectOutput ObjOut = new ObjectOutputStream(new FileOutputStream(f));

Above code has been used to create the instance of the ObjectOutput class with the ObjectOutputStream( ) constructor which takes the instance of the FileOuputStream as a parameter.

The ObjectOutput interface is used by implementing the ObjectOutputStream class. The ObjectOutputStream is constructed to serialize the object.

Deserializing an Object in java

The opposite operation of the serialization is called deserialization i.e. to extract the data from a series of bytes is s known as deserialization which is also called inflating or unmarshalling.

ObjectInputStream extends java.io.InputStream and implements ObjectInput interface. It deserializes objects, arrays, and other values from an input stream. Thus the constructor of ObjectInputStream is written as:

ObjectInputStream obj = new ObjectInputStream(new FileInputStream(f));

Above code of the program creates the instance of the ObjectInputStream class to deserialize that file which had been serialized by the ObjectInputStream class. The above code creates the instance using the instance of the FileInputStream class which holds the specified file object which has to be deserialized because the ObjectInputStream() constructor needs the input stream.


Serialization is the process of turning a Java object into byte array and then back into object again with its preserved state. Useful for various things like sending objects over network or caching things to disk.

Read more from this short article which explains programming part of the process quite well and then move over to to Serializable javadoc. You may also be interested in reading this related question.


Return the file as an Object : http://www.tutorialspoint.com/java/java_serialization.htm

        import java.io.*;

        public class SerializeDemo
           public static void main(String [] args)
              Employee e = new Employee();
              e.name = "Reyan Ali";
              e.address = "Phokka Kuan, Ambehta Peer";
              e.SSN = 11122333;
              e.number = 101;

                 FileOutputStream fileOut =
                 new FileOutputStream("/tmp/employee.ser");
                 ObjectOutputStream out = new ObjectOutputStream(fileOut);
                 System.out.printf("Serialized data is saved in /tmp/employee.ser");
              }catch(IOException i)

    import java.io.*;
    public class DeserializeDemo
       public static void main(String [] args)
          Employee e = null;
             FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn);
             e = (Employee) in.readObject();
          }catch(IOException i)
          }catch(ClassNotFoundException c)
             System.out.println("Employee class not found");
          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);


Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object. It is mainly used to travel object’s state on the network (known as marshaling).


Serialization (Of an Object)

In the context of data storage and transmission, serialization is the process of converting a data structure or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and “resurrected” (de-serialized) later in the same or another computer environment.



  • Object-Serialization is process of converting the state of an object into steam of bytes, so that Serializable-obects cannot read and understood by humans so we can acheive security.
  • Object-Deserialization is the process of getting the state of an object and store it into an object(java.lang.Object). but before storing its state it check weather serialVersionUID form input-file/network and .class file serialVersionUID are same. if not throw java.io.InvalidClassException.
  • On providing implementation of serializable interface(marker interface), we are providing information to compiler to use
    Java Serialization mechanism to serialize this object.
  • A Java object is only serializable. if its class or any of its superclasses implements either the java.io.Serializable interface
    or its subinterface, java.io.Externalizable.

Class impements Serializable Interface

class Employee implements Serializable{ 
        private static final long serialVersionUID = 2L;
        int eno; 
        String name;        
        static int id; 
// static information belongs to Entire-Class not for a perticular-object. but we can access through object.

        transient String password; //  sensitive information 

        private String classlevel;  
        public String getClasslevel() {     return classlevel;  }
        public void setClasslevel(String classlevel) {      this.classlevel = classlevel;   }

Main Class

public class SrializationDemo {
        public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
            Employee e = new Employee();
            e.eno = 12;
            e.name = "Yash";                
            e.password = "confidential";
            e.id = 19;
            e.setClasslevel("Class Spesific data");
            serialization_TXT(e); // Serializable obects cannot  read and understood by humans.
        public static void serialization_TXT(Object o) throws IOException{  

            FileOutputStream fileOut = new FileOutputStream(new File("SerializationOut.txt"));
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(o); // To store any object via serialization mechanism we call readoject() / writeObect()
            System.out.println("Data Stored in SerializationOut.txt file");
        public static void de_serialization() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
            FileInputStream fileIn = new FileInputStream(new File("SerializationOut.txt"));
            ObjectInputStream in = new ObjectInputStream(fileIn);
            Object o =  in.readObject();                
        //  Employee e = (Employee) Class.forName(o.getClass().getName()).newInstance(); // creating new object.                
            String className = o.getClass().getName();// First Find the incoming Object Name, then type cast.
            Employee e = (Employee) o; // then type cast to that Correct-Class-type. So that we can get data and store it into this current object.
    // java.io.InvalidClassException: core.Employee; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2

            System.out.println(e.eno + "\t" +e.name +"\t" + e.password + "\t"  + e.id +"\n"+ e.getClasslevel());


  • We use transient keyword, where the member variable not to be serialized when it is persisted to streams of bytes.(so that the object will not be stored into a file or transferred through the network).

This question can also make you understand a tip about Serialization:

What will go wrong if we make all java objects Serializable Since Serializable is an empty interface and Java
provides robust serialization once you add implements Serializable
why didn’t they make everything serializable and that’s it?

The answer could be found at “Why Java needs Serializable interface?” which describes this could lead to security problems and also encapsulation breaks.


|*| Serializing a class : Converting an object to bytes and bytes back to object (Deserialization).

class NamCls implements Serializable
    int NumVar;
    String NamVar;

|=> Object-Serialization is process of converting the state of an object into steam of bytes.

  • |-> Implement when you want the object to exist beyond the lifetime of the JVM.
  • |-> Serilized Object can be stored in Database.
  • |-> Serializable-obects cannot be read and understood by humans so we can acheive security.

|=> Object-Deserialization is the process of getting the state of an object and store it into an object(java.lang.Object).

  • |-> Before storing its state it check weather serialVersionUID form input-file/network and .class file serialVersionUID are same.
    &nbsp&nbspIf not throw java.io.InvalidClassException.

|=> A Java object is only serializable, if its class or any of its superclasses

  • implements either the java.io.Serializable interface or
  • its subinterface, java.io.Externalizable.

|=> Static fields in a class cannot be serialized.

class NamCls implements Serializable
    int NumVar;
    static String NamVar = "I won't be serializable";;

|=> If you do not want to serialise a variable of a class use transient keyword

class NamCls implements Serializable
    int NumVar;
    transient String NamVar;

|=> If a class implements serializable then all its sub classes will also be serializable.

|=> If a class has a reference of another class, all the references must be Serializable otherwise serialization process will not be performed. In such case,
NotSerializableException is thrown at runtime.


I found this good answer from here :-

Imagine you want to save the state of one or more objects. If Java didn’t have serialization, you’d have to use one of the I/O classes to write out the state of the instance variables of all the objects you want to save. The worst part would be trying to reconstruct new objects that were virtually identical to the objects you were trying to save. You’d need your own protocol for the way in which you wrote and restored the state of each object, or you could end up setting variables with the wrong values. For example, imagine you stored an object that has instance variables for height and weight. At the time you save the state of the object, you could write out the height and weight as two ints in a file, but the order in which you write them is crucial. It would be all too easy to re-create the object but mix up the height and weight values—using the saved height as the value for the new object’s weight and vice versa.
The purpose of Serialization is to help us achieve whatever complicated scenario we just witnessed in an easier manner.

Working with ObjectOutputStream and ObjectInputStream

The magic of basic serialization happens with just two methods: one to serialize objects and write them to a stream, and a second to read from the stream and deserialize the object.

ObjectOutputStream.writeObject() - serialize and write

ObjectInputStream.readObject() - read and deserialize

The java.io.ObjectOutputStream and java.io.ObjectInputStream classes are considered to be higher-level classes in the java.io package, and as we learned in the previous chapter that means that you’ll wrap them around lower-level classes, such as java.io.FileOutputStream and java.io.FileInputStream. Here’s a small program that creates an object, serializes it, and then deserializes it:

import java.io.*;

class Car implements Serializable { } // 1

public class SerializeCar {
public static void main(String[] args) {
Car c = new Car(); // 2
try {
FileOutputStream fs = new FileOutputStream("testSer.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(c); // 3
} Catch (Exception e) { e.printStackTrace(); }

try {
FileInputStream fis = new FileInputStream("testSer.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
c = (Car) ois.readObject(); // 4
} Catch (Exception e) { e.printStackTrace(); }

Let’s take a look at the key points in this example:

  1. We declare that the Car class implements the Serializable interface. Serializable is a marker interface; it has no methods to implement.

  2. We make a new Car object, which as we know is serializable.

  3. We serialize the Car object c by invoking the writeObject() method. First, we had to put all of our I/O-related code in a try/Catch block. Next we had to create a FileOutputStream to write the object to. Then we wrapped the FileOutputStream in an ObjectOutputStream, which is the class that has the magic serialization method that we need. Remember that the invocation of writeObject() performs two tasks: it serializes the object, and then it writes the serialized object to a file.

  4. We de-serialize the Car object by invoking the readObject() method. The readObject() method returns an Object, so we have to cast the deserialized object back to a Car. Again, we had to go through the typical I/O hoops to set this up. – See more