Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use Serialization judicially

 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone throw light on how does serialization breach singleton pattern?I am not able to figure out.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic