Serialization in Java


Serialization-
Convert the state of the object into byte stream, so that byte stream can be transferred or retrieved back to the copy of the object from file or the network.

or

Serialization - is a process of converting object into a binary format - passed to network disk or send to other JVM on the network


The entire process is JVM independent and so the object can be serialized on one platform and can be
 de-serialized on any other platform

High Level class
ObjectInputStream
    public final void writeObject(Object x) throws IOException
ObjectOutputStream
    public final Object readObject() throws IOException, ClassNotFoundException
The return type of this method is of type Object class you need to cast is to appropriate object

File->FileOutputStream or FileInputStream -> ObjectOutputStream or ObjectInputStream

Serialization Example -
public class SerializationExample implements Serializable{

/**

*/
private static final long serialVersionUID = -3521498400546337287L;
String name;
int age;
//SerializationSuper ss ; this is not serialize so better comments it
SerializationExample(String name , int age){
this.name = name;
this.age = age;
}

public static void main(String[] args) {
doSerialization();
SerializationExample sex = doDeSerialization();
System.out.println(sex.age);
System.out.println(sex.name);
}

private static SerializationExample  doDeSerialization() {
SerializationExample sex = null;
File file = new File("C:/abc");
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
sex = (SerializationExample)ois.readObject();
} catch ( IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return sex;

}

private static void doSerialization() {
SerializationExample soe = new SerializationExample("ank", 400);
File file = new File("C:/abc");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(soe);
os.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}


Some Points-
1- Class to be Serializable either itself implements  Serializable or any class in the parent hierarchy does.
2-If the parent class implements Serializable then all the child class to that will also be Serializable whether or not they implements Serializable.
3- if you are using any class object as a instance variable in the class which is not Serializable  then whole class throws java.io.NotSerializableException: at the serialization process
4-if your parent implements Serializable.  so the hell you will automatically Serializable.
but you dont want this then you have to 

private void writeObject(ObjectOutputStream out) throws IOException{
    throw new NotSerializableException("Plz Dont Serialize me");
}

private void readObject(ObjectInputStream in) throws IOException{
    throw new NotSerializableException("Hah I am not Serialize");

}


5- if you dont want any instance variable Serializable.  then mark it a transient and enjoy 
6-if the parent is not Serializable.  but you want yourself to be Serializable. then 
there is nothing to worry the default constructor of your parent will run at the time of deserialization so they got the default of null value

6-Static variable can not be Serializable.  it shows no error but return the current value of the static variable 
7-All the members of the collection must be Serializable.

8-if the class is  Serializable. but its parent is not then what will happen to the inherited members 
    java serialization process only continues in the object hierarchy till the class is serializable 
then in the deserialization all the ONLY default constructor of all the parent class in the hierarchy  will run and if the class don't have default constructor please provide otherwise it will gonaa blast at runtime





Comments