jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Is there any cost associated with the implementation of Serializable interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is there any cost associated with the implementation of Serializable interface?" Watch "Is there any cost associated with the implementation of Serializable interface?" New topic
Author

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

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 547
    
    2



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?


Regards
KumarRaja

Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19758
    
  10

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

Joined: Sep 20, 2010
Posts: 4101
    
  19

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.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4541
    
    8

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
 
subject: Is there any cost associated with the implementation of Serializable interface?
 
It's not a secret anymore!