• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

K &B: "Serialization Is Not for Statics"

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the 310-055 guide page 456 it is said that
"Static variables are NEVER saved as part of the object's state´┐Żbecause they do
not belong to the object!"

But i tried the below program and i am seeing that the static variable value is saved.Can somebody explain what is the reason.also if anybody knows how serialization works on static variables then that would be helpful.



[ December 21, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When it is done in the above program the class Animal is not unloaded from the JVM and reloaded into the JVM. So the modified static variable is seen in the reconstructed object.

Split the program into two programs, one to serialize and the other to deserialize. You will then see that the static variable is not restored with the modified value but to the initial value.

In my opinion, K & B should rewrite this misleading example program and split the program into two programs.
[ December 21, 2006: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try with:

Dog.java

DeSerializeDog.java:

The output for SerializeDog:
before: Fido 35 and test is :modified-test and Animal test is :Modified animal test

The output for DeSerializeDog:
after: Fido 42 and test is :modified-test and Animal test is :Animal test


(added tags)
[ December 22, 2006: Message edited by: Barry Gaunt ]
 
Ravi Nistala
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Nandhini and Barry for the explaining the problem.Initially i was confused after reading the guide. I think the K&B book needs to be changed to explain this.

I leave upto them to decide.This is just my opinion.

Ravi.n
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Actually, the discussion of statics and serialization comes after the code you're discussing. The program you're looking at was used to demonstrate a different aspect of serialization - what happens when instances of serializable subclasses (with non-serializable superclasses) are serialized and deserialized.

Perhaps we could add another test program to demonstrate statics and serialization, but I don't think we should "overload" the one test program to try to explain these two distinct topics.

Thoughts?

Bert
 
Ravi Nistala
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bert for your response.By all means , i am not a writer, so i may be wrong.Please feel free to comments to my approach.
Having said that .
There are several ways to do it
1) You can add/modify exisiting text to say what has been described above.
2) i have prepared a small example for this sub-topic. I think this example is of a small foor print than described above.you all free to modify according your need.
3) You can add this as a excercise at the end of your chapter.
4) you can combine (options "1" and "3") or (option 1 and 2) .

I think somehow it has to reach the audience.
Below is the code example which i mentioned. I tested it on my m/c and it works.
 
reply
    Bookmark Topic Watch Topic
  • New Topic