Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should we make our POJO's Serializable in Java?

I've seen a lot of questions on how to Serialize POJO's in java using different frameworks, but I wanted to really understand the need of making our POJO's Serializable in the first place. You use JavaScript to handle the POJO objects or Spring or any other framework - why is it that we always have to make our POJO's Serializable ?

Is it something that has to be done on a GOOD PRACTICE or a BEST PRACTICE?

If it is not the case, what is the advantage that we would be able to leverage by Serializing POJO classes?

There are a couple of threads that discuss about this, but I'm not really satisfied with the explanation / answers!

Can anyone here, shed some light on this concept please?

like image 346
N00b Pr0grammer Avatar asked Sep 30 '15 09:09

N00b Pr0grammer


2 Answers

You serialize POJO's when you usually need to:

  1. Transmit them through some medium (Web Service, etc)
  2. Store them on some medium. (This in turn devolves into how you are going to store them: XML, binary, etc).

You do not always have to serialize them, it depends on what ever it is you are doing.

For instance, if you have a web application with the concept of a User object, wherein a user has a user name, and maybe some preferences it might not make sense to make that class serializable. However, if you expose a web service through which 3rd parties can extract user information, then that class would need to be serializable so that it can be transmitted to said 3rd parties.

like image 95
npinti Avatar answered Nov 14 '22 21:11

npinti


Serialization is needed when you have to convert the Object to byte Stream.

Byte Streaming is needed when you transmit the object with other applications or when you have to store that object.

So if you are using other formats like JSON for passing data, you won't need to serialize your objects.

like image 33
Austin p.b Avatar answered Nov 14 '22 22:11

Austin p.b