Serialize and De-Serialize Object in Java

Introduction

What is Serialization? What it Does Why Do we need it?

Ans: Serialization is a marker interface which doesn't have any method but still classes implements it.

Serialization means maintaining state of an Object ,basically it saves the value of an object into memory.

But still we can do it if we want to save it writing it in memory but why do our class need to implement it.

The main use of serializable is when you are passing Object from one server to another it generates and hexadecimal code and this is to insure that Object reaches to another Server maintaining its state.


Following and Example How to serialize and De serialize a Java Class Object:


First make a Java Class as mentioned below:




Test.Java

import java.io.Serializable;


public class Test implements Serializable{

	public String a;
	
	public Test()
	{
		System.out.println("Constructor");
	}
	
	static{
		System.out.println("static");
	}

	public String getA() {
		return a;
	}

	public void setA(String a) {
		this.a = a;
	}
	

	
}

Here we have made a Test Class Which has a String variable a and also we made an default constructor just to see whether it is called during serialization and deserialization or not


and also Static block is written to check its role.


Now we will do SERIALIZATION of Test.JAVA

SerializiaingObject.JAVA

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;


public class SerializiaingObject {

	
	public static void main(String []  args)
	{
		Test t=new Test();
		try{
			t.setA("Serialized String");
			FileOutputStream fileOut =new FileOutputStream("test.ser");
			ObjectOutputStream out =new ObjectOutputStream(fileOut);
			out.writeObject(t);
			out.close();
			System.out.println("serialized");
			fileOut.close();
			System.out.println(t.a);
			t.setA("After Serialization");
		}catch(IOException i)
		{
			i.printStackTrace();
		}


	}
}

DeserializingObject.JAVA

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;


public class DeserializingObject {


	public static void main (String[] args)
	{

		Test e = null;
		try
		{
			FileInputStream fileIn =new FileInputStream("test.ser");
			ObjectInputStream in = new ObjectInputStream(fileIn);
			e = (Test) in.readObject();
			in.close();
			System.out.println("Deserialized");
			System.out.println(e.a);
			fileIn.close();
			e.a="test";
		}catch(IOException i)
		{
			i.printStackTrace();
			return;
		}catch(ClassNotFoundException c)
		{
			System.out.println(".Test class not found.");
			c.printStackTrace();
			return;
		}

		System.out.println(e.a);
	}
}

Conclusion

Now run the code and Check the output in case of any query please comment.

Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working