| Author |
Serialization is not for staitics
|
Jerry Ragland
Ranch Hand
Joined: Apr 26, 2005
Posts: 33
|
|
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
Joined: Aug 03, 2002
Posts: 7729
|
|
|
Try it in two different programs, the first to serialize the object and the second to get it back in again...
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Chris Hurst
Ranch Hand
Joined: Oct 26, 2003
Posts: 370
|
|
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.
|
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
|
 |
Jerry Ragland
Ranch Hand
Joined: Apr 26, 2005
Posts: 33
|
|
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
Joined: Aug 03, 2002
Posts: 7729
|
|
|
I still recommend that you try two different programs.
|
 |
Jerry Ragland
Ranch Hand
Joined: Apr 26, 2005
Posts: 33
|
|
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
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Jerry Ragland
Ranch Hand
Joined: Apr 26, 2005
Posts: 33
|
|
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
Joined: Aug 03, 2002
Posts: 7729
|
|
|
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
Joined: Feb 15, 2007
Posts: 557
|
|
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.
|
Thanks & Regards,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
|
 |
victor kamat
Ranch Hand
Joined: Jan 10, 2007
Posts: 247
|
|
It is interesting to note that for the following class when it is deserialized (after being serialized) that i = 11 and k = 0;
|
 |
 |
|
|
subject: Serialization is not for staitics
|
|
|