Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there any cost associated with the implementation of Serializable interface?

 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


We know that, if we anticipate objects are passed by value over a remote method calls, or should we choose to serialize/deserialize the objects or want to save the object as a bytes to some file (and many other usecases), we are required to have our class implement Serializable. But lately I was just curious, if any cost is associated with the implementation of this marker interface. What if, we do not have any such requirements that are needed for Serialization, but still want to make it a serializable. I understand that there is no need to do anything that is absolutely not needed, but what if decide to make it Serializable. Would this decision cause some sort of unexpected behaviour?
 
Paul Clapham
Sheriff
Pie
Posts: 20187
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean behaviour which you would not expect, or behaviour which somebody else would not expect?

Obviously we have no way of predicting what you might expect.
 
Stephan van Hulst
Bartender
Pie
Posts: 4838
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I avoid Serializable like the plague. It completely breaks all encapsulation. Instead, if I want to save my objects, I usually add a methods like this to my class:
Or in the case of immutable objects:
Serializable makes your class' private fields part of the specification, which is absolutely horrible.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, it would probably be a good idea to implement Externalizable. That way you get the benefits of it being Serializable (e.g. works with ObjectOutputStreams), but the class controls the read/write.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic