Lost Password?

  #1 (permalink)  
Old 05-07-2008, 04:34 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 25
Posts: 4,536
Last Blog:
PHP: list()
Rep Power: 50
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Tutorial: Java Serialization

Java Serialization


What is Serialization?

Simply put serialization is a method to deflate an object to store it on the system.


So how do you do it?
Simply implement the Serializable interface. Once you implement that interface, not only does your base class deflate, even you child classes deflate! What do I mean by all this, well read on.


Implementing Serialization
1) Implement the interface serialzable.

Code:
class Example implements Serializable.
The above step tells the complier that you are going to implement serialization.
Which basically means that the object is going to be saved.


2) Time to write the object to the disk.
Code:
FileOutputStream fOut = new FileOutputStream("SerializedFile.txt"); 
ObjectOutputStream outSt = new ObjectOutputStream(fOut); 
outSt.writeObject(b1);
This simply creates an output stream, pipes it up with an object output stream and writes the deflated object to disk. The name of the file is SerializedFile.txt.


3) Then you decide to bring the objects back to life. So you follow the reverse process, which, naturally is called deserialization!


Code:
FileInputStream fIn = new FileInputStream("SerializedFile.txt"); 
ObjectInputStream inSt = new ObjectInputStream(fIn); 
b3 = (ObjectType)inSt.readObject();
4) What if you have something top secret, like your password which should not be saved. You mark it as transient!

Code:
 transient int i;
My guess is that you understood nothing till now. And rightfully so, its too much to take in one go. So here is a detailed theoretical explanation.

  • Serializaation is used to save an object and all the objects that inherit from the one serialized. They too are saved.
  • The objects whose references are in the serialized object is also saved!
  • The reason all these objects have to be saved is because when the main object is brought back to life, the references it points to must not be null. They should point to some valid objects.
  • That can only happen if all the objects referenced are saved with the base object.
  • In case you want that something should not be saved, you simply prefix it with the word transient.
  • When a serialized object is brought back to life, the transient variables are initialized to null.
  • Static variables, functions and classes cannot be serialized.
  • One very peculiar thing to note is that the constructors are not called again when the object is deserialized. This will be made amply clear by the example given below.

The Code
Code:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Base implements Serializable{
    Base()
    {
        System.out.println("In Base  Constructor");
    }
    public void function()
    {
        System.out.println("In a function");
    }
}
class Derived extends Base{
    Derived()
    {
        System.out.println("In Derived  Constructor");
    }
}

public class SerializeObj {
    public static void main(String[] args) {
        Base b1 = new Base();
        Base b2 = new Base();
//---------------Writing to disk------------------------
        try{
                 b1.function();
                 b2.function();
                 FileOutputStream fOut = new FileOutputStream("SerializedFile.txt");
                 ObjectOutputStream outSt = new ObjectOutputStream(fOut);
                 outSt.writeObject(b1);
                 outSt.writeObject(b2);
                 outSt.close();
        } 
        catch(Exception e){
        e.printStackTrace();
    } 
    System.out.println("End of serialization");
    System.out.println("Begin Deserialization");
//---------------Reading from disk----------------------
        try{
                 FileInputStream fIn = new FileInputStream("SerializedFile.txt");
                 ObjectInputStream inSt = new ObjectInputStream(fIn);
                 Base b3 = (Base)inSt.readObject();
                 Base b4 = (Base)inSt.readObject();
                 b3.function();
                 b4.function();
                 inSt.close();
        } 
        catch(Exception e){
             e.printStackTrace();
        } 
    }
}
Output
Code:
In Base Constructor
In Base Constructor
In a function
In a function
End of serialization
In a function
In a function
Questions or Comments?
Ask your questions or comments here!
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply

Tags
java, serialization, tutorial



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Java Database Connectivity Jordan Java Tutorials 0 05-07-2008 04:28 PM
Tutorial: Starting Java Using Netbeans Jordan Java Tutorials 0 04-05-2008 02:45 PM
Java tutorial : my sql with java Arkie Java Tutorials 2 04-05-2008 12:51 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 03:05 PM


All times are GMT -5. The time now is 10:26 AM.

Contest Stats

dargueta ........ 93.00000
John ........ 87.50000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads