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

2019-02-04 10:29发布

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?

9条回答
我命由我不由天
2楼-- · 2019-02-04 10:36

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.

查看更多
神经病院院长
3楼-- · 2019-02-04 10:44

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.

查看更多
看我几分像从前
4楼-- · 2019-02-04 10:44

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

查看更多
男人必须洒脱
5楼-- · 2019-02-04 10:45

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.

查看更多
孤傲高冷的网名
6楼-- · 2019-02-04 10:48

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.

查看更多
Melony?
7楼-- · 2019-02-04 10:49

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.

查看更多
登录 后发表回答