• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Serialization

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q class Animal implements Serializable { Dog d; }

class Dog { }

When we attempt to serialize an instance of Animal class then what is the result:
a)One object is serialized
b)An exception is thrown at runtime.

Ans: one object is serialized..

My Doubt is what will be the bahaviour of Dog instance while serialization...
--->Since its not instantiated so the compiler will not throw any exception(or will it throw the "NotSerializable Exception"???)
--->Its not transient.If it would have marked transient then compiler would skip it...

So now in the given case when the Animal instance will be deserialized what would compiler will do with that instance???

Thanks!!!
Swapnil
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Swapnil Trivedi:
Q class Animal implements Serializable { Dog d; }

class Dog { }

When we attempt to serialize an instance of Animal class then what is the result:
a)One object is serialized
b)An exception is thrown at runtime.

Ans: one object is serialized..

My Doubt is what will be the bahaviour of Dog instance while serialization...
--->Since its not instantiated so the compiler will not throw any exception(or will it throw the "NotSerializable Exception"???)
--->Its not transient.If it would have marked transient then compiler would skip it...

So now in the given case when the Animal instance will be deserialized what would compiler will do with that instance???

Thanks!!!
Swapnil



I think, it should throw an exception...
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you won't get an object serialized.

You will get a NotSerializableException
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you will not get an exception. As it is written, there is no Dog object for the serializer to write (dog is uninitialized in Animal). The serializer certainly knows how to write null and uninitialized values to an output, so there will be no error.

The serialization error will occur if you initialize dog - ie. give it a value - because the Serializer will not know how to serialized the Dog class (it does not implement Serializable).

This example should help you with the behaviour:



Run that and you will get the output:

Got a dog, and his name is: Fido

Change the line maked by //1 to:



Run it and you will get two serialization errors (one for the read and one for the write).
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys....Now I got it



Regards
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic