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:
- What data may cause the
NotSerializableException
? - How should I know that I am not supposed to add the
implements Serializable
clause for my class?
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.
It all depends on your need.
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.
You can serialize your Object to be used by other JVM working on different machine.
When you are talking about
NotSerializableException
it is throw when you want to serialize an object, which has not been marked asSerializable
- that's all, although when you extend non serializable class, and addSerializable
interface it is perfectly fine.There is no data that can't be serialized.
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
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.
NotSerialisable
exception is thrown when something in your serializable marked as serializable. One such case can be:Here super is not mentioned as serializable so will throw
NotSerializableException
.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: