views:

784

answers:

5

Hi,

If a similar question is already posted on stackoverflow, pls just post the link.

What is the need to implement Serializable interface (with no methods) for objects which are to be serialized ? The Java API says - - If its not implemented then it will throw java.io.NotSerializableException.

That's because of the following code in ObjectOutputStream.java

............................

writeObject0(Object obj, boolean unshared){
.............
 } else if (cl.isArray()) {
     writeArray(obj, desc, unshared);
        } else if (obj instanceof Serializable) {
     writeOrdinaryObject(obj, desc, unshared);
        } else {
     throw new NotSerializableException(cl.getName());
        }
................

But my question is why its necessary to implement Serializable and thereby inform or tell Java/JVM that a class can be serialized. (Is it only to avoid the exception ?).

In this is the case, If we write a similar functionality which writes objects to streams without the check of whether the class in an instanceOf Serializable, Will the objects of a class not implemneting Serializable serialized ?

Any help is appreciated.

+4  A: 

This question has some good answers to this.

jwoolard
+2  A: 

It's a good question. The Serializable is know as a marker interface, and can be viewed as a tag on a class to identify it as having capabilities or behaviours. e.g. you can use this to identify classes that you want to serialise that don't have serialVersionUid defined (and this may be an error).

Note that the commonly used serialisation library XStream (and others) don't require Serializable to be defined.

Brian Agnew
+2  A: 

It is needed so that the JVM can know whether or not a class can be safely serialized. Some things (database connections for example) contain state or connections to external resources that cannot really be serialized.

Also, you'll want to make sure that you put a serialVersionUID member in every serializable class to ensure that serialized objects can be de-serialized after a code change or recompile:

// Set to some arbitrary number.  
// Change if the definition/structure of the class changes.
private static final long serialVersionUID = 1;
Eric Petroelje
A: 

The reason is that not all classes can be serialized. Examples:

  • I/O stuff: InputStream, HTTP connections, channels. They depend on objects created outside the scope of the Java VM and there is no simple way to restore them.

  • OS resources like windows, images, etc.

Aaron Digulla
A: 

The serialization allows you to save objects directly to binary files without having to parse them to text, write the string out and then create a new object, and parse the string inputs when reading back in. The primary purpose is to allow you to save objects with all their data to a binary file. I found it to be extremely useful when having to work with linked lists containing lots of objects of the same type and I needed to save them and open them.

That's an answer to the question "Why is serialization useful?"
Michael Myers
But it also answers why you have to mark it as serializable in order to use binary files and to write/read objects.