Java serialization concept and Example

This entry is part 28 of 34 in the series Core Java Course

Here i will learn and teach you what is Serialization in Java and how to write code for the same. I am learning this because i am going for an Interview. :P

What is Serialization

Serialization is a process in which current state of Object will be saved in stream of bytes. As byte stream create is platform neutral hence once objects created in one system can be deserialized in other platform.

What is the use of Serialization

As written above serialization will translate the Object state to Byte Streams. This Byte stream can be used for different purpose.

  • Write to Disk
  • Store in Memory
  • Sent byte stream to other platform over network
  • Save byte stream in DB(As BLOB)

 

Serialization and Deserialization in Java

Now we know what is serialization. But in terms of Java how this serialization will work and how to make a class serializable. Java has already provided out of the box way(java.io.Serializable  Interface) to serialize an Object. If you want any class to be serialized then that class needs to implement give interface.

Note*: Serializable Interface is a Marker Interface. Hence there is no method in Serializable interface.

 

Code for Serialization of Java Class

Employee.java

 

SerializaitonClass.java

 

DeserializationClass.java

First run “SerializaitonClass” and you will get “employee.txt” file created.

Second run “DeserializationClass” and java will deserialize the class and value will be printed in console.

Output would be

 

Bullet Points

  • Serialization interface needs to be implemented in order to make object serialized.
  • Transient instance variable doesn’t serialized with Object state.
  • If Super class implements Serializable then sub class are also Serializable automatically.
  • If Super class is not serializable then when sub class is de serialized then super class’s default constructor will be invoked. Hence all variable will get default value and reference will be null.

In Next Article i will discuss some advance part of Serializable.

 

Series Navigation<< Java String TutorialJava serialization concept and Example Part II >>

16 Comments Java serialization concept and Example

  1. New Dev

    when i run “SerializaitonClass” im getting following error. Not sure where is this going wrong
    SerializationClass.java:10: error: cannot find symbol
    Employee emp = new Employee();
    ^
    symbol: class Employee
    location: class SerializationClass
    SerializationClass.java:10: error: cannot find symbol
    Employee emp = new Employee();
    ^
    symbol: class Employee
    location: class SerializationClass
    2 errors

    Reply
    1. Gagandeep

      Error : “Cannot find symbol”
      It is encountered when either the symbol is not visible or found out of scope.
      Perhaps the class “Employee” could also not be imported.

      Reply
    2. Rahul Mandhan

      You should create and import Employee class then run your serialization program sure it would run successfully…

      Reply
  2. Anantha

    Hi In this post there is typo. Serializable Interface is a Market Interface. Please change into Marker Interface.

    Reply
  3. Nandini

    hey here you have given one file name know “./employee.txt”, where that file will store actually.

    Reply
  4. Surya

    not working, getting ClassCastException in the line “emp = (Employee) in.readObject();”

    can you help ?

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *