简体   繁体   中英

With out implementing a Serializable interface is it possible to make a class persistable in java?

In Java we implement an interface Serializable that defines no method (also called a marker interface).

But suppose I have an interface without any method just like a Serializable interface, can I make it work just like that, meaning that I would be able to use my interface instead of the Serializable ?

Thanks

Only Serializable will mark an object as being compatible with Java's built-in serialization machinery.

You can create other empty interfaces, but they won't mean the same thing. Each interface is distinct, even if it defines the same set of methods.

No. If you want to be able to use Java Serialization, your objects need to implement Serializable .

If you want to use other serialization tools, (ie: Hibernate , SimpleXML , XStream ), that is always a possibility, but those generally involve adding annotations, xml files, or other configurations.

If you want your object be serializable it must implement java.io.Serializable .

You could also create your own interface or class that extend from Serializable and then another class/interface extend from that one.

No. Serializable is a special interface, and you can't use a different one with the built-in serialization mechanism. You could roll your own entire system, but it'd be a big job.

If you don't implement Serializable you will not be able to use Java serialization to persist it. But you are more than welcome to implement your own persistence that is not relying on Java serialization.

Serializable is called a marker, but only because classes like ObjectOutputStream and ObjectInputStream use the reflection API to check for it. There is no special magic there; if you wanted to implement your own alternative serialization strategy, you could also create your own interface and use that as the marker you wished to respect. Although, you should consider whether it would actually be better design to use the existing Java marker (Serializable). I believe some other alternatives to the built-in Object stream do this.

If you merely wish to take complete control over how your objects are serialized into bytes, you can do so without rewriting the ObjectXXXStream classes, by using Serializable subclass Externalizable. And in fact, this is commonly done, since the automatic serialization afforded by Serializable ungracefully dies with the smallest change to the SerialVersionUID, which, unless supplied, is calculated automatically from various signatures of the class - making Serializable unsuitable for must persistent storage, since most kinds of changes (ie during a routine upgrade) to the underlying class will prevent reloading the serialized data.

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