Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is implementing java.io.Serializable even in simple POJO Java classes a best practice?

In general, is it a best practice to have simple POJO Java classes implement java.io.Serializable?

like image 396
CBass Avatar asked Oct 20 '08 17:10

CBass


2 Answers

Generally not. Joshua Bloch says to implement Serializable judiciously. A summary of drawbacks that he describes:

  • decreases flexibility of changing class implementation later - the serialized form is part of the class's API
  • makes some bugs and security holes more likely - an attacker can access class internals within the serialized byte stream
  • increases test burden - now you have to test serialization!
  • burdens authors of subclasses - they have to make their subclasses Serializable too

Of course, sometimes you need a POJO to implement Serializable, say for RMI, but if the need isn't there, your code will be simpler and more secure without it.

like image 130
bhavanki Avatar answered Sep 18 '22 23:09

bhavanki


Only if you need to be able to serialise them. It's not worth the effort otherwise.

like image 25
Dan Dyer Avatar answered Sep 21 '22 23:09

Dan Dyer