2

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...

Community
  • 1
  • 1
Pedantic
  • 1,368
  • 10
  • 39
  • 70
  • 1
    See [Why Java needs Serializable interface?](http://stackoverflow.com/questions/441196/why-java-needs-serializable-interface). Implementing Serializable tells the runtime the class can be safely serialized. – Matthew Flaschen Jun 07 '10 at 09:44

5 Answers5

1

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
  • 376,812
  • 128
  • 561
  • 623
1

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
  • 18,367
  • 27
  • 104
  • 155
1

Serialization is more complicated then that, if you ever tried to save a few native variables like int, long, etc. into a stream you called them literally.

int i=100;
stream.write(i);

The thing about serialization, every Serializable object has a serialVersionUID that is unique to each object that is Serializable, and via reflection + a very sophisticated mechanizem (to prevent saving the same instance of an objects over again) it breaks down the object and saves it as bytes, just like my example, only there is a predefined very general mechanizem that does it.

which means, in your example it would look into the DBAccessRequest Class object check for which variable are there and if they are Serializable too it would perform the same process on them, and chop them to their Serializable core, and save them as bytes.

This is all metaphorical to explain the general concept of a process which is very complicates, very intresting and worth investigating (you should look it up also):

How Serialization works1

How Serialization works2

Hope this help,

Adam.

TacB0sS
  • 10,106
  • 12
  • 75
  • 118
0

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.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
0

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
  • 21,619
  • 17
  • 70
  • 86
  • 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 clear thanks again – Pedantic Jun 07 '10 at 10:05