Java: What can and what can't be serialized?

纵然是瞬间 提交于 2019-11-27 01:31:45

问题


If the Serializable interface is just a Marker-Interface that is used for passing some-sort of meta-data about classes in java - I'm a bit confused:

After reading the process of java's serialization algorithm (metadata bottom-to-top, then actual instance data top-to-bottom), I can't really understand what data cannot be processed through that algorithm.

In short and formal:

  1. What data may cause the NotSerializableException?
  2. How should I know that I am not supposed to add the implements Serializable clause for my class?

回答1:


When you are talking about NotSerializableException it is throw when you want to serialize an object, which has not been marked as Serializable - that's all, although when you extend non serializable class, and add Serializable interface it is perfectly fine.

There is no data that can't be serialized.




回答2:


First of all, if you don't plan to ever serialize an instance of your class, there is no need to even think about serializing it. Only implement what you need, and don't try to make your class serializable just for the sake of it.

If your object has a reference (transitive or direct) to any non-serializable object, and this reference is not marked with the transient keyword, then your object won't be serializable.

Generally, it makes no sense to serialize objects that can't be reused when deserialized later or somewhere else. This could be because the state of the object is only meaningful here and now (if it has a reference to a running thread, for example), or because it uses some resource like a socket, a database connection, or something like that. A whole lot of objects don't represent data, and shouldn't be serializable.




回答3:


Anything your Serializable class has in it that is not Serializable will throw this exception. You can avoid it by using the transient keyword.

Common examples of things you can't serialize include Swing components and Threads. If you think about it it makes sense because you could never deserialize them and have it make sense.




回答4:


All the primitive data types and the classes extend either Serializable directly,

class MyClass extends Serializable{
}

or indirectly,

class MyClass extends SomeClass{
}

SomeClass implements Serializable.

can be serialized. All the fields in a serializable class gets serialized except the fields which are marked transient. If a serializable class contains a field which is not serializable(not primitive and do not extend from serializable interface) then NotSerializableException will be thrown.

Answer to the second question : As @JB Nizet said. If you are going to write the instance of a class to some stream then and then only mark it as Serializable, otherwise never mark a class Serializable.




回答5:


You need to handle the serialization of your own Objects.

Java will handle the primitive data types for you.

More info: http://www.tutorialspoint.com/java/java_serialization.htm




回答6:


NotSerialisable exception is thrown when something in your serializable marked as serializable. One such case can be:

class Super{}
class Sub implements Serializable
{
Super super;

Here super is not mentioned as serializable so will throw NotSerializableException.




回答7:


After reading the process of java's serialization algorithm (metadata bottom-to- top, then actual instance data top-to-bottom), I can't really understand what data cannot be processed through that algorithm.

The answer to this is certain system-level classes such as Thread, OutputStream and its subclasses which are not serializable. Explained very well on the oracle documents: http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html

Below is the abstract:

On the other hand, certain system-level classes such as Thread, OutputStream and its subclasses, and Socket are not serializable. Indeed, it would not make any sense if they were. For example, thread running in my JVM would be using my system's memory. Persisting it and trying to run it in your JVM would make no sense at all.




回答8:


More practically, no object can be serialized (via Java's built-in mechanism) unless its class implements the Serializable interface. Being an instance of such a class is not a sufficient condition, however: for an object to be successfully serialized, it must also be true that all non-transient references it holds must be null or refer to serializable objects. (Do note that that is a recursive condition.) Primitive values, nulls, and transient variables aren't a problem. Static variables do not belong to individual objects, so they don't present a problem either.

Some common classes are reliably serialization-safe. Strings are probably most notable here, but all the wrapper classes for primitive types are also safe. Arrays of primitives are reliably serializable. Arrays of reference types can be serialized if all their elements can be serialized.




回答9:


What data may cause the NotSerializableException?

In Java, we serialize object (the instance of a Java class which has already implemented the Serializable interface). So it's very clear that if a class has not implemented the Serializable interface, it cannot be serialized (then in that case NotSerializableException will be thrown).

The Serializable interface is merely a marker-interface, in a way we can say that it is just a stamp on a class and that just says to JVM that the class can be Serialized.

How should I know that I am not supposed to add the implements Serializable clause for my class?

It all depends on your need.

  1. If you want to store the Object in a database, you can serialize it to a sequence of byte and can store it in the database as persistent data.

  2. You can serialize your Object to be used by other JVM working on different machine.



来源:https://stackoverflow.com/questions/16851070/java-what-can-and-what-cant-be-serialized

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