smruti ranjan
smruti ranjan

Reputation: 781

What is use of making classes serializable?

  1. I have noticed many of the library classes "ArrayList", "String" even the exceptions are having a serialVersionUID. Why they have made it like this. Whats the practical use of doing that.FYI I am familiar with the concept of Serialization. Please point out the practical purpose of it.

For your reference find the serialversionUid for ClassCastException

public class ClassCastException extends RuntimeException {
private static final long serialVersionUID = -9223365651070458532L;

Where these object's state going to persist? And where will these objects state going to be retrieved ?

  1. I am currently working in a project where we are making REST controllers whose input and output parameters will be JSON.We are creating simple POJOs for i/p and o/p parameters.I have seen people making those POJOs serializable .Whats the point in doing that ?

But I havent seen **out.readObject** or out.writeobject which is used to write and read the state of object.Will the POJO's state persist just making it serializable? If yes where it will be stored?

Upvotes: 1

Views: 106

Answers (1)

Andreas
Andreas

Reputation: 159086

If you want the full story, read the spec: Java Object Serialization Specification.

[...] many of the library classes "ArrayList", "String" even the exceptions are having a serialVersionUID. Why they have made it like this.

To support backwards compatibility when reading objects that were written in an older version of the class. See Stream Unique Identifiers.

Where these object's state going to persist?

Wherever you decide. See Writing to an Object Stream.

And where will these objects state going to be retrieved ?

Wherever you put it. See Reading from an Object Stream.

[...] input and output parameters will be JSON. [...] I have seen people making those POJOs serializable. Whats the point in doing that ?

None. JSON is not using Java serialization. Java serialization creates a binary stream. JSON creates text.

Will the POJO's state persist just making it serializable? If yes where it will be stored?

No, see above.

Upvotes: 3

Related Questions