Emailid
Password
         
  
    Forgot password

New user Sign Up


Object resolution

       Current Rating:  50%                                                     Total Members Rated:  1
                                                                     Send To Friend

  

Object resolution

 

 

Introduction

 

This article explains the benefits of object serialization, and how to implement it in programs. A serious task that applications have to perform is to save and restore data. Whether it is a word processing application that saves documents to disk, a utility that remembers its configuration for next time, or a game that sets aside world domination for the night, the ability to store data and later retrieve it is a vital one. Without it, software would be little more effective that the typewriter - users would have to re-type the data to make further modifications once the application egress. With object serialization, your Java applets and applications can save and load the state of objects to disk or over a network.

 

Code

 

Code writing and saving the data is tedious dull effort. The programmer must create a specification document for the proposed file structure. The programmers have to apply save and restore functions that convert object data to & from primitive data types, and test it with sample data. If the application later requires new data to be stored, the file specification must be modified, as well as the save and restore methods. Take it from someone who's been there - creating save & restore functions is not a amusing mission.

 

Serialization

 

Serialization is the solution for code writing trouble. Object serialization takes an object's state, and converts it to a stream of data for you. With object serialization, it's an easy task to take any object, and make it persistent, without writing custom code to save object member variables. The object can be restored at a later time, and even a later location. With persistence, we can move an object from one computer to another, and have it maintain its state. This specialty in Java is so much simple to apply.

 

Object Serialization

 

Extra methods are not needed to be added to implement the interface. But the purpose of the interface is to identify at run-time which classes can be safely serialized, and which cannot. Any object whose class implements the java.io.Serializable interface can be made persistent with only a few lines of code. The programmer has to only add the implements keyword to your class declaration, to identify your classes as serializable. Java makes it easy to serialize objects. And it is much more helpful and smooth to apply.

For the application of Java we can write the object to any Output Stream, such as to disk or a socket connection once a class is serializable. To achieve this, we must first create an instance of java.io.ObjectOutputStream, and pass the constructor an existing OutputStream instance.

 

// Write to disk with FileOutputStream

FileOutputStream f_out = new

            FileOutputStream("myobject.data");

 

// Write object with ObjectOutputStream

ObjectOutputStream obj_out = new

            ObjectOutputStream (f_out);

 

// Write object out to disk

obj_out.writeObject ( myObject );

 

Any Java object that implements the serializable interface can be written to an output stream including those that are part of the Java API. And also any objects that are referenced by a serialized object will also be stored. This means that arrays, vectors, lists, and collections of objects can be saved in the same fashion - without the need to manually save each one. This can lead to considerable time and code savings. And this is very effective.

 

Serialized position and Renovating objects

 

Reading objects back is an easy thing. One vicious circle is that at runtime, you cannot be sure what type of data to expect. A data stream containing serialized objects may contain a mixture of different object classes, so you need to explicitly cast an object to a particular class. If you've never cast an object before, the procedure is relatively straightforward. First check the object's class, using the instanceof operator. Then cast to the right category.

 

// Read from disk using FileInputStream

FileInputStream f_in = new

            FileInputStream("myobject.data");

 

// Read object using ObjectInputStream

ObjectInputStream obj_in =

            new ObjectInputStream (f_in);

 

// Read an object

Object obj = obj_in.readObject();

 

if (obj instanceof Vector)

{

            // Cast object to a Vector

            Vector vec = (Vector) obj;

 

            // Do something with vector....

}

Few problems with Serialization

 

 

Whenever new fields are added to an object, they will be saved automatically, without requiring modification to your save and restore code. But on some cases this behavior is not pleasing. If a password member variable might not be safe to transmit to third parties over a network connection, and might need to be left blank. In this case, the transient keyword can be used. The transient field indicates that a particular member variable should not be saved. Though not used often, it's an important keyword to remember. It is easy to serialize an object.

 

public class UserSession implements

         java.io.Serializable

{

            String username;

            transient String password;

}

 

Benefits of Serialization

 

1. It formulates easier for objects to travel over a network connection.

 

2. You are able to pertain serialization to a variety of tasks with relatively little effort.

 

3. It lessens time taken to write code for save and restoration of object or application state.

 

4. It removes complexity of save and restore operations, and avoiding the need for creating a new file format.

 

Applet

 

Applets also benefit from serialization. An applet can simple reload a configuration object whose member variables contain all the information needed to execute, rather than specifying a long list of parameters, or performing time consuming initialization and parsing, It's not just useful for Java applications - even applets can make benefit, by loading their configuration details or parameters. With a little imagination, serialization may just have a place in your next plan.

 

Conclusion

 

The amount of code needed to save and restore every field of an object is complex and repetitive work. Java's support for object serialization makes the implementation of persistent objects extremely easy. It is certainly possible to write your own serialization mechanism, the simplicity of that provided by Java would be hard to beat.


                           Rate This Article:   

Author is Offline
  Author: Chudain Soovay
       


Comments Posted
Label
Subject Author Status Date

 

Post Comment

Related Articles
Fundamentals on .NET Framework
N-Tier in ASP.NET or other .NET apps
Software Development Outsourcing (Offshore ) to India
Make your web site ‘perfect and Search Engine Friendly for Google and Yahoo
Color combination of your Web site



Home | About Us | Site Map | Privacy Policy