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

末鹿安然 提交于 2020-01-05 07:36:29

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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 (i.e. during a routine upgrade) to the underlying class will prevent reloading the serialized data.



来源:https://stackoverflow.com/questions/6751661/with-out-implementing-a-serializable-interface-is-it-possible-to-make-a-class-pe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!