简体   繁体   English

为什么要将类标记为可序列化?

[英]Why its required to mark a class as serializable?

If a similar question is already posted on stackoverflow, pls just post the link. 如果在stackoverflow上已经发布了类似的问题,请发布链接。

What is the need to implement Serializable interface (with no methods) for objects which are to be serialized ? 对于要序列化的对象,需要实现Serializable接口(没有方法)是什么? The Java API says - - If its not implemented then it will throw java.io.NotSerializableException. Java API说 - - 如果没有实现,那么它将抛出java.io.NotSerializableException。

That's because of the following code in ObjectOutputStream.java 这是因为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. 但我的问题是为什么它必须实现Serializable,从而通知或告诉Java / JVM一个类可以被序列化。 (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 ? 在这种情况下,如果我们编写一个类似的功能,它将对象写入流而不检查实例中的类是否可以序列化,那么类的对象是否会实现Serializable序列化?

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. Serializable被称为标记接口 ,可以被视为类的标记,以将其标识为具有功能或行为。 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). 例如,您可以使用它来标识要序列化的类,这些类没有定义serialVersionUid(这可能是一个错误)。

Note that the commonly used serialisation library XStream (and others) don't require Serializable to be defined. 请注意,常用的序列化库XStream(和其他)不需要定义Serializable。

It is needed so that the JVM can know whether or not a class can be safely serialized. 需要它以便JVM可以知道类是否可以安全地序列化。 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: 此外,您需要确保在每个可序列化类中放置serialVersionUID成员,以确保在代码更改或重新编译后可以对序列化对象进行反序列化:

// 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. I / O东西:InputStream,HTTP连接,通道。 They depend on objects created outside the scope of the Java VM and there is no simple way to restore them. 它们依赖于在Java VM范围之外创建的对象,并且没有简单的方法来还原它们。

  • OS resources like windows, images, etc. 操作系统资源,如窗口,图像等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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