my dog learned polymorphism*
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is serialization still useful? " Watch "Is serialization still useful? " New topic
Author

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?

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

Joined: Feb 28, 2007
Posts: 1707
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!


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

cmbhatt
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
Marshal

Joined: Mar 22, 2005
Posts: 42371
    
  64
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.


Ping & DNS - my free Android networking tools app
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

Hibernate does not use XML to store objects.

Thanks Ulf!
 
jQuery in Action, 2nd edition
 
subject: Is serialization still useful?