Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do transfer objects need to implement Serializable?

I realized today that I have blindly just followed this requirement for years without ever really asking why. Today, I ran across a NotSerializableException with a model object I created from scratch and I realized enough is enough.

I was told this was because of session replication between load-balanced servers, but I know I've seen other objects at session scope that do not implement Serializable. Is this the real reason?

like image 774
sma Avatar asked Mar 16 '10 01:03

sma


People also ask

Why we need to implements Serializable?

Implement the Serializable interface when you want to be able to convert an instance of a class into a series of bytes or when you think that a Serializable object might reference an instance of your class. Serializable classes are useful when you want to persist instances of them or send them over a wire.

What is the purpose of object Serialisation?

Serialisation is used in the cases where you want to save an object state and convert that object into byte stream, objects need to be serilazed. While you're running your application, all of its objects are stored in memory (heap memory).

What will happen if we don't implement Serializable?

The Student would not be Serializable, and it will act like a normal class. Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link.


1 Answers

Because in order to be transferred across a wire they need to be serialisable to a form that can be put on the wire.

Like binary, or xml, Json, or simillar

There's more info here... Should any domain object, not be serializable?

like image 86
Preet Sangha Avatar answered Oct 06 '22 12:10

Preet Sangha