• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialization, can someone elaborate?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From K&B:
"If you serialize a collection or an array, every element must be serializable! A single non-serializable element will cause serialization to fail. Note also that while the collection interface are not serializable, the concrete collection classes in the Java API are."

What confuses me a little is the last sentence. Should it be interpreted
as if I inherit, and/or implement, a class/interface that is not serializable, it is still OK to have elements of that class in e.g. an
array and serialize that array, knowing off course that any inherited
members will not be part of the serialization process?
The rules for non-serializable superclasses thus applies also to interfaces
that I implement, though interfaces have no member variables and falls out of the serialization process by design.
I think I know the answer but I just want to double-check to make absolutely sure.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi cowboys,

Hans Beck�rus wrote:


" ... Note also that while the collection interface are not serializable, the concrete collection classes in the Java API are."

What confuses me a little is the last sentence.




Say, you have a serializable class containing this member:

Then you can serialize objects of that class. That's because the object type of list is ArrayList, which is serializable. It is not important here that the reference type is List, which is not serializable.

However when you have a class like

... it is not possible to use a Banana object as a member in a serializable class.
Unless you mark it transient.

I think, that's all K&B wanted to express with this sentence.


Considering arrays:
I find arrays a bit confusing when it comes to serialization.
e.g.

prints out:
oneObject is Serializable? false
objectArray is Serializable? true



So, an array always IS-A Serializable.
But when you have an array of non-serializable objects as a member in your class (implementing Serializable), you cannot serialize it. Famous NotSerializableException.

An array generally is serializable and it's up to you to see that all elements in the array are objects of classes that implement Serializable.



Yours,
Bu.
 
reply
    Bookmark Topic Watch Topic
  • New Topic