Tuesday, January 29, 2008

Very Basic Of Serialization

SERIALIZATION-
Serialization means saving the state of an Object or we can say that freezing the state and write down to a file:


Analogy:
Suppose we have an Object of class BankAccount. At any time Object can give us Net Amount PRESENT in account. In January Lets says balance in BankAccount was 100$. There were two activities in BankAccount:

> Deposit of 500$ in February. Net Amount = 600$
> With drawl of 300$ in March. Net Amount = 300$

Now we are in  March, The account holder wants to compare Net Amount in February with Net Amount in March but the issue is that we can only get to know PRESENT Net Amount, which is 300$. How to solve this issue?
Solutions
1) Declare one variable say FEB_AMOUNT, save the value of Net Amount in February to FEB_AMOUNT. Later in March we can compare FEB_AMOUNT to PRESENT Net Amount (300$).

Problem : There could be a lot of aspects of BankAccount such as holder name, Bank Name, Deposit Currency, Deposit Date etc which one wants to compare, in this case we may have to declare large number of variable to temporary store the values of different aspects.


2)In February, We can write down the value of all properties of BankAccount in the file. Later in March we can read the file compare it against PRESENT value of BankAccount.
This thing is called Serialization in JAVA, ObjectOutputStream does same, It creates one file and note down the state of the Object in that file in some encrypted form and Later OBjectInputStream can be used to read that.

Points to be remembered:
Requirement : Only those classes which implement Serializable Interface can be serialized. Serializable interface is a market interface which means - no method.
Transient: If we don't want to save any instance properties of class we can simply marked it as transient, Its value will not be written by ObjectOutputStream.
Static: Through Serialization we save the state of an OBJECT, whereas STATIC means CLASS level. so static variable can not be serialized..

9 comments:

kunal said...

grt site mannn!!!!

Anonymous said...

Thanks. I never thought serialization is so easy to understand before reading this blog.

Anonymous said...

its so great solution to learn Serialization.

Anonymous said...

its so great solution to learn Serialization.

Anonymous said...

excellent example..

Taufique.S.Shaikh said...

This is the great example of serialization.Explained in such a simple manner.Really enjoyed reading it,creativity at its best.

mona said...

its market written.cn u correct the typo..otehrwise great article

mona said...

its market written.cn u correct the typo..otehrwise great article

Eric Simmons said...

Great and Useful Article.

Online Java Training

Online Java Training from India

Java Online Training

Online Java Training India

Java Training Institutes in Chennai

Java Course in Chennai

Java Training in Chennai

Java Interview Questions

Java Tutorials

Core Java Interview Questions