32

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?
Community
  • 1
  • 1
MordechayS
  • 1,552
  • 2
  • 19
  • 29
  • TL;DR: So I think what I'm reading here from a combination of several answers is that the basic Java types are Serializable, but if you have them in a class then that class (or one of its parent classes) needs to be Serializable. If any class (or parent) is not explicitly marked Serializable, then it isn't. If you mark a class as Serializable then all of its members each need to follow these same rules for themselves. – Andrew Aug 14 '18 at 16:34
  • It seems, however, that if a class is marked Serializable, then any **member** class instances (of that Serializable class) that contain **only** Serializable types as their members, including other classes that only contain Serializables, etc., will also be Serializable without needing to be explicitly declared as such. This I gather from my own code. – Andrew Aug 14 '18 at 16:39
  • 1
    @Andrew Reffering to the last note about "member classes" - every class that should (and can...) be serialized - needs to be marked as Serializable. Any other option is at least a very bad practice. – MordechayS Aug 14 '18 at 19:58

9 Answers9

16

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.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
14

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.

Michal Borek
  • 4,584
  • 2
  • 30
  • 40
  • 2
    The only one that actually grasped my meaning: I did not want to know **when** to use the 'serializable' interface. I just wanted to understand what data is serializable and what data isn't. If some of you guys (@JB Nizet etc) can actually add a small explanation about: If any data is, ultimately, made out of primitives - and those are serializable - why did the java guys created this interface? – MordechayS Jun 02 '13 at 02:35
  • 2
    @MordechayS Serializable is a marker interface to distinguish between objects you want to serialize from those you don't want to. there are many types of objects that do not make sense when serialized. e.g. objects of type Connection / File etc. These represent resources tied to specific machines and won't make any sense if serialized. Thus the marker interface. – Gautam Jan 03 '19 at 04:35
  • @Michal Borek, you said very well that, there is no data that cant be serialized, then why do we need Serializable attribute, why cant serializable attribute the implicit behavior(I am just curios). – Shankar S Aug 24 '21 at 07:51
  • 1
    @ShankarS that's because you may want to disallow the serialisation of the class. What I meant at that time was that you can serialise any object until you provide appropriate serialisation mechanism. In some cases you might need to provide custom serialisers and in some cases the implicit serialisation is enough. Java is an explicit language, usually you need to state what is obvious - e.g. the type of the local variable (that tends to change in newer versions - the "var" keyword, but still this is how Java is built). – Michal Borek Aug 25 '21 at 19:05
6

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.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
5

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.

Sachin Gorade
  • 1,427
  • 12
  • 32
4

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

LeigerGaming
  • 504
  • 1
  • 4
  • 17
2

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.

Pawan Patil
  • 1,067
  • 5
  • 20
  • 46
Jain
  • 201
  • 2
  • 2
2

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.

Vishu Gupta
  • 611
  • 5
  • 10
0

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.

Pavan Kumar K
  • 1,360
  • 9
  • 11
  • What exactly happens if a class that is serializable have an object reference of non-serializable class but null. Can you explain a bit more about how null is handled while serialization? – Akhil Dad Nov 17 '15 at 06:54
0

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.

Pang
  • 9,564
  • 146
  • 81
  • 122