How to make a class serialized?

Technology: java
AskedIn:
Topics:
Type:
  • Implement java.io.Serializable interface to the class whose object we need to be serialized
  • The ObjectOutputStream class contains writeObject() method for serializing an Object
  • The ObjectInputStream class contains readObject() method for de-serializing an Object
  • static and transient data members are not saved in the process of Serialization
  • If a parent class has implemented Serializable interface then the child class does not need to implement it again
  • final variables are directly serialized by their values, so there is no use/impact of declaring final variables as transient
  • SerialVersionUID - it should be static, final and long - todo
  • Associated objects must be implementing Serializable interface - todo

import java.io.*;

 

public class SerializationDemoUpdate implements Serializable {

   private static final long serialVersionUID = 12345567L;

   transient int a;

   static int b;

   transient final int c;

   String name;

   int age;

 

   public SerializationDemoUpdate(int a, int b, int c, String name, int age) {

       this.age = age;

       this.a = a;

       this.b = b;

       this.c = c;

       this.name = name;

   }

 

   public void printData() {

       System.out.println("name: " + this.name);

       System.out.println("age: " + this.age);

       System.out.println("a: " + this.a);

       System.out.println("b: " + this.b);

   }

 

   public static void main(String[] args) {

       SerializationDemoUpdate obj = new SerializationDemoUpdate(10, 20, 30, "Cortane", 40);

       String fileName = "file.txt";

       // serialization

       try {

           FileOutputStream fileOutputStream = new FileOutputStream(fileName);

           ObjectOutputStream objOutputStream = new ObjectOutputStream(fileOutputStream);

           objOutputStream.writeObject(obj);

           objOutputStream.close();

           fileOutputStream.close();

           System.out.println("serializing\n" + "data before serializing");

           obj.printData();

           obj.b = 2000;

       } catch (IOException e) {

           System.out.println("IOException");

       }

       // deserialization

       SerializationDemoUpdate objToDeserialize = null;

       try {

           FileInputStream fileInputStream = new FileInputStream(fileName);

           ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

 

           objToDeserialize = (SerializationDemoUpdate) objectInputStream.readObject();

           objectInputStream.close();

           fileInputStream.close();

           System.out.println("De-serializing\n"+"data after deserialization");

           objToDeserialize.printData();

       } catch (IOException e) {

           System.out.println("IOException caught");

       } catch (ClassNotFoundException e) {

           System.out.println("ClassNotFoundException is caught");

       }

 

 

   }

 

}

 

Output

serializing

data before serializing

name: Cortane

age: 40

a: 10

b: 20

De-serializing

data after deserialization

name: Cortane

age: 40

a: 0 

b: 2000