views:

137

answers:

5

Possible Duplicate:
Why Java needs Serializable interface?

I know the serialization process but have't implemented it.

In my application i have seen there are various classes that has been implemented serilizable interface. consider following class

public class DBAccessRequest
implements Serializable
{
    private ActiveRequest request = null;
    private Connection connection = null;
    private static Log log = LogFactory.getLog(DBAccessRequest.class);

    public DBAccessRequest(ActiveRequest request,Connection connection)
    {        
        this.request = request;
        this.connection = connection;
    }

    /**
     * @return Returns the DB Connection object.
     */
    public Connection getConnection() {
        return connection;
    }

    /**
     * @return Returns the active request object for the db connection.
     */
    public ActiveRequest getRequest() {
        return request;
    }
}

just setting request and connection in constructor and having getter setter for them. so what is the use of serilizable implementation over here...

A: 

From Sun:

We all know the Java platform allows us to create reusable objects in memory. However, all of those objects exist only as long as the Java virtual machine1 remains running. It would be nice if the objects we create could exist beyond the lifetime of the virtual machine, wouldn't it? Well, with object serialization, you can flatten your objects and reuse them in powerful ways.

So I think that the reason why this is marked as serializable was that once that the JVM is up and running again, the data that was previously used is loaded and used again. This might be useful so as to avoid having the user enter the same data each and every time that the application loads.

npinti
+1  A: 

It's what is called a marker interface; it's used to define types that are serializable in Java. From the API:

java.io.Serializable: Serializability of a class is enabled by the class implementing [this] interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

See also:

polygenelubricants
A: 

Rozer, I guess you mean why the class is marked Serializable and no functionality related to serialization is put there except getters and setters.

The class might not directly implement the serializable functions.

  1. The class which derives your DBAccessRequest class might implement them; or
  2. The class which has an instance of DBAccessRequest might needed to be serialized which necessitate DBAccessRequest to be serializable.

I don't have any other explanation for this.

bdhar
thanks Bdhar, means you want to say the class having the object of DBAccessRequest need to be serialize so we are doing so(implement serializable in DBAccessRequest) but that is not a case over here, and i hve sees many classes hving the same scenario.still confusion..ok can u help me any scenario where i can use serializable and can see everything so picture would be more clearthanks again
Rozer
A: 

Layman would say : "Serializable? It's how to tell Java, that I need to breake this object to bytes and safely store somewhere else, and vice versa"

Xorty
+1  A: 
TacB0sS