aspose file tools*
The moose likes Java in General and the fly likes Use Serialization judicially Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Use Serialization judicially" Watch "Use Serialization judicially" New topic
Author

Use Serialization judicially

Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Hi All,

Can someone help me in knowing , what points a person should consider while implementing serialization.What could be the side-effects if serialzation is not implemented wisely.

I'll be obliged if someone can anser me.
Venkat Sadasivam
Ranch Hand

Joined: May 10, 2008
Posts: 139
By implementing Serializable interface it will not harm your code performance unless you try to save the object using ObjectOutputStream.

This article talks about the need for Serializable interface in Java.


“Any fool can write code that a computer can understand. Good programmers write code that humans can understand. ”<br>
-Martin Fowler
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
The side-effects could include breaching the Singleton design pattern. Also if you alter your class, your SerialVersionUID might change, and it would then be impossible to Serialise or Deserialise objects of older versions of the class.
Bound to be more side-effects.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Originally posted by Campbell Ritchie:
The side-effects could include breaching the Singleton design pattern.

Fortunately, the readResolve method can help you with that:

This code even shows how to handle lazy evaluation - just use getInstance() instead of instance in readResolve().


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
And somebody on beginner's found that you can have two references to an object before serialisation and have two references to different objects after!
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Can someone throw light on how does serialization breach singleton pattern?I am not able to figure out.
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Originally posted by Patricia Samuel:
Can someone throw light on how does serialization breach singleton pattern?I am not able to figure out.


Someone can write a singleton instance to disk, and then read it back up, effectively getting a new instance. Even though the constructor is private, the serializable tools have special access to create instances of a class regardless.
Source


Thanks and Regards
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

Serialization should not be used for any kind of long-term storage.

One problem with serialization is that it works only as long as you don't change the classes that you serialize. Suppose you are building an application that saves its data by serializing a bunch of objects. And now suppose that you are developing a new version of the application. If you change anything in the classes that are serialized, the new version of your application will no longer be able to read the files saved by the old version. You'll get errors because the serialized objects don't match the classes anymore.

Serialization is useful for temporary, short-term storage or for transporting objects over a network (with RMI, for example), but it's not suitable as a general way to store data.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Unless you specify the serialVersionUID manually, and make sure you don't add, remove or change any non-transient fields. That includes changing the order.
 
 
subject: Use Serialization judicially