• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Serialization

 
Ranch Hand
Posts: 52
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I was studying serialization for OCJP exam from the book written by Jeanne Boyarsky and Scott Selikoff when on page number 448  I found a question :

Why shouldn't every class be marked Serializable ?

One of the answers said : The compiler will throw an exception if certain classes are marked Serializable
I thought this was a correct answer but according to the book this is not a correct I don't understand why, check this code:


This code throws a java.io.NotSerializableException when executed. Here the MyClass class if marked Serializable will throw an exception so the option should be correct.


Next although I understood serializability I did not understand one thing the book said. What does this mean ?

"Java will call the constructor for the first non-serializable no-arguments parent class during deserialization, skipping any constructors and default initializations for serializable classes in between"

Can someone explain this sentence possibly with an example because it seems too difficult to understand for me.

By the way a big thank you to this forum and all the awesome people in it. It has proven to be life saviour for me in clearing all my doubts....
 
Saloon Keeper
Posts: 15727
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code doesn't throw any exceptions when run.

Arpan Ghoshal wrote:Can someone explain this sentence possibly with an example because it seems too difficult to understand for me.



For this you must first have written an object of type D to a file named D.bin.

The expected output is:
 
Arpan Ghoshal
Ranch Hand
Posts: 52
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:That code doesn't throw any exceptions when run.



Yes I don't know how I got a NotSerializableException before but I want to clarify something the book says :

"The requirement for properly marking an object serializable may involve nested objects.For example if a Cat class is marked serializable and contains a reference to a Tail object,then the class definition for the tail object must also be marked as Serializable."

Consider this..


Here the Cat class contains an instance variable containing an object of class Tail but the Tail class is not marked Serializable then how does this code work ?
Also does a class need to marked serializable to be written to a file using writeObject() ? I think the answer is no then I can't understand what is actually the need for Serialization ?

Stephan van Hulst wrote:The expected output is:



Why doesn't it print D also ?
 
Arpan Ghoshal
Ranch Hand
Posts: 52
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arpan Ghoshal wrote:
Yes I don't know how I got a NotSerializableException before



Okay I found out why I got the exception before. If a field is not serializable like Object ob=new Object(); and the class containing the field is marked serializable then the code works and there is no exception but the problem arises if we try to write an object of the class in a file using writeObject() then this will throw a java.io.NotSerializableException.


 
Stephan van Hulst
Saloon Keeper
Posts: 15727
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arpan Ghoshal wrote:Here the Cat class contains an instance variable containing an object of class Tail but the Tail class is not marked Serializable then how does this code work ?


You didn't try to write an object of type Cat.

Also does a class need to marked serializable to be written to a file using writeObject() ? I think the answer is no then I can't understand what is actually the need for Serialization ?


Yes. If you want to write an object to an ObjectOutputStream, it needs to be Serializable and all its instance fields that are not transient also need to be Serializable.

Why doesn't it print D also ?


Because D is Serializable.

Why shouldn't every class be marked Serializable ?

One of the answers said : The compiler will throw an exception if certain classes are marked Serializable


You've already seen that the compiler doesn't throw an exception when you mark a class as Serializable. It's only when you try to write an instance at runtime, and not all non-transient instance fields are also Serializable.
 
What's gotten into you? Could it be this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic