---
title: "Serialization in Java"  
description: "Serialization is a process of converting an object into a stream."  
author: "Anupam Mishra"  
published: 2015-11-17  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/11917/serialization-in-java  
category: "java"  
tags: ["java", "jms", "j2ee", "ejb", "hibernate"]  
reading_time: 3 minutes  

---

# Serialization in Java

It is mainly used in Hibernate, RMI, JPA, EJB, JMS technologies.\

The main advantage of [Serialization](https://www.mindstick.com/blog/71/what-is-serialization) is to be travel object's state on the network ([known as](https://answers.mindstick.com/qa/38482/name-the-world-s-lightest-material-which-was-manufactured-by-isro-scientists-at-the-vikram-sarabhai-space-centre-thiruvananthapuram-and-is-also-known-as-blue-air) marshaling).\
The reverse [operation](https://yourviews.mindstick.com/view/83916/why-is-it-necessary-to-sterilize-operation-theater) of serialization is called [deserialization](https://www.mindstick.com/interview/2462/what-is-deserialization). Deserialization is the process of reconstructing the object from the serialized state.\

The String class and all the [wrapper classes](https://www.mindstick.com/articles/12133/wrapper-classes-constructors-and-methods)’ implements java.io.[Serializable](https://www.mindstick.com/forum/158890/explain-the-difference-between-parcelable-and-serializable-interfaces-in-android) interface by default.

Serializable is a [marker interface](https://www.mindstick.com/interview/23422/marker-interface-in-java) (has no body). It is just used to "mark" java classes which support a certain capability. It must be implemented by the class whose object you want to persist.\
Let’s take an example, we are going to serialize the object of Emp & MyClient class. The writeObject () method of ObjectOutputStream class provides the [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) to serialize the object. We are saving the state of the object in the file named Employee.txt. and deserialized in MyServer class.\

##### Note:-

1. Only non-static data member are saved via serialization process.2. Static data member and transient data member are not saved via serialization process.3. If you does not want to save the value of any non- static data member via serialization process then make it transient.4. If parent class has implemented the Serializable interface then [child class](https://www.mindstick.com/forum/159389/how-to-remove-child-class-from-active-div) does not need to implement but vice versa is not true.

> ```
>       import java.io.*;//Emp.javapublic class Emp  implements Serializable{transient int a;//it will not be serialized.static int b;String name;int age;int z;Emp(String name,int age,int a,int b,int z){    this.name=name;    this.age=age;    this.a=a;    this.b=b;    this.z=z;} }class  MyClient{  public MyClient()    {        try{            Emp e1= new Emp("Anupam Mishra",24,5,10,15);            FileOutputStream fout=new FileOutputStream("Employee.txt");    ObjectOutputStream out=new ObjectOutputStream(fout);      out.writeObject(e1);              out.flush();        }catch(Exception e){System.out.println(e);}    }    public static void main(String...ss)    {new MyClient();    System.out.println("Serialization in MyClient is success");}}class MyServer{    ObjectInputStream dis;    public MyServer()    {        try{            dis=new ObjectInputStream(new FileInputStream("Employee.txt"));            Emp z= (Emp)dis.readObject();            System.out.println("Name :"+z.name);            System.out.println("Age :"+z.age);            System.out.println("a :"+z.a);            System.out.println("b :"+z.b);            System.out.println("Z :"+z.z);        }catch(Exception e){System.out.println(e);}        }        public static void main(String...ss)    {new MyServer();    System.out.println("Deserialized in MyServer is completed");}}
> ```
>
> ##### Output:
>
> \
>
> ![Serialization in Java](https://www.mindstick.com/mindstickarticle/87823b7f-bc60-4f14-9093-a74b35e6595f/images/e0771b84-5b89-437e-a0a3-07f23bce0521.png)

---

Original Source: https://www.mindstick.com/articles/11917/serialization-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
