File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Is serialization still useful? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is serialization still useful? " Watch "Is serialization still useful? " New topic

Is serialization still useful?

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

It seems like at some point in the future that serialization will be deprecated.

Somebody told me that since serialization is binary and once you store it to persistent storage or if you're talking to another computer running Java with different versions of objects of the same class (such as using RMI), that this is problematic since once you start doing these types of things that it becomes difficult to add instance variables to your objects.

Doesn't this violate encapsulation?

I don't remember where I heard or read this, but there is some speculation that serialization as we know it now (to a binary format) should be replaced with serialization to XML files which are more well-suited to handling version differences.

Is there any truth to these rumors?

[ June 03, 2007: Message edited by: Barry Gaunt ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710
As I know serialization is useful specially when you need to transfer
objects over the network so that you could reconstruct an object on the
another machine. Serialization is for storing object state to the persistent
storage for later use.

I see in various web applications, when it comes that you need to send an
object of particular class over the network, your class must be serializable.

Not sure what you say, that XML will replace binary transformation of object. As I know in Hibernate technology it happens.

Let's see what others say!

[ June 03, 2007: Message edited by: Chandra Bhatt ]

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This is beyond the scope of SCJP. Moving to Java In General (Intermediate).

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Doesn't this violate encapsulation?

Interesting point. I suppose it does to some degree. Having the requirement that the receiver must have the same version of the class as the sender adds version information to the public knowledge about the object.

When you start going beyond your JVM, say to a persistance tool or passing objects over a network, you can introduce a lot of details with that potential. The boundaries between the ideal object model and the non-OO world are always a bit messy.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Actually, you do not need to have the same class file on each end; you need to have compatible class files on each end. Serialization includes a versioning mechanism that allows you to modify your classes as needed and still be able to read old serialized data files. Google "serialversionuid".

JAXB is the most "official" way to do serialization to XML; there are loads of commercial and non-commercial tools that do it, too. XML serialization is handy for long-term storage and communication between Java and non-Java code. For communicating over a wire between Java programs, though, a binary wire format is always going to be most efficient. Whether its JRMP with serialization, or IIOP, or something else, there's a niche for it, and it's a different niche than is served by serialization to XML.

[Jess in Action][AskingGoodQuestions]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Not sure what you say, that XML will replace binary transformation of object. As I know in Hibernate technology it happens.

That's what the java.beans.XMLEncoder/XMLDecoder classes are for. But they only work for objects that conform to the JavaBeans specification.

Hibernate does not use XML to store objects.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710

Hibernate does not use XML to store objects.

Thanks Ulf!
I agree. Here's the link:
subject: Is serialization still useful?
It's not a secret anymore!