简体   繁体   中英

Why its required to mark a class as serializable?

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.

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

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;

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM