Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization is not for staitics

 
Jerry Ragland
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this topic in K&B book during study. To understand what will be the behaviour if a static variable is includede I wrote a code with a static variable. I thought the value will not be include and will give default value to my surpirse it returned the value correctly.



If I get the o/p like this why Serialization can't be applied for staics.

Thanks in advance.
-Jerry.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it in two different programs, the first to serialize the object and the second to get it back in again...
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or just do another s.display (); between writing to ser.txt and reading back in the results, this does another i++;

... code snippet follows ...


... code continues ...

giving this output ...

The integer value 11
The integer value 12
The integer value with serialized object 12

which basically proves the static wasn't written to ser.txt as when you read it back it should have gone back to 11 but its not stored in the file so it doesn't.
 
Jerry Ragland
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chris, for the explanation. So when a static variable is serailized it will take the current value of the variale and store it. But we can't rely on this stored value as when retrived back it will again show the current value and not the previously recorded value.

-Jerry.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still recommend that you try two different programs.
 
Jerry Ragland
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
I still recommend that you try two different programs.


Barry tried with 2 different programs -



I am finding it hard to conclude. Could you please help me in concluding the behaviour with statics.

Regards,
-Jerry.
 
Henry Wong
author
Marshal
Pie
Posts: 20992
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when a static variable is serailized it will take the current value of the variale and store it.


No... as you have previously mentioned, static variables which are part of the serialization process -- they will not be stored.

But we can't rely on this stored value as when retrived back it will again show the current value and not the previously recorded value.


No... since static variables are not serialized, they will also *not* be deserialized, reliablly or otherwise. Remember, there is only *one* copy of a static variable, so in your first example, the static variable is correct, not because it was deserialized correctly, but because you didn't change the value, period.

Henry
 
Jerry Ragland
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, I don't understand why it behaves differently when in a same program and in a different program.




When I deserialize the object in the same program from where I serialized it I get result as 12. When I again deserialize the same file in a seperate class I get a different result (as 10) ... really confusing


-Jerry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use a single program the class gets loaded once and stays around within the JVM. When you use two programs the class gets loaded freshly by the second program into a new JVM. That's why you see the static variable at its proper initial value.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jerry,

Static variables have a single copy per class.
Static variable is a class variable. not instance variable.
We serialize the instance of class. not class.

1)In Program posted first> Imagine that there is no serialization, Now see Is there any problem with output(i) 11. No problem!

2)In the second program>
In DoSerial2 class,
2.1)serial s = null;
2.2)s = (serial)is.readObject();
Above steps cause Class "serial" load again.
At the static variable loads with 10.

Hope you got it.
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is interesting to note that for the following class



when it is deserialized (after being serialized) that i = 11 and k = 0;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic