This week's book giveaway is in the Design forum.
We're giving away four copies of Experimentation for Engineers: From A/B testing to Bayesian optimization and have David Sweet on-line!
See this thread for details.
  • 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Static variable - serialization Q

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is Q25 from ExamLab SCJP5.



The output is "4 5 0".

I would of thought that the static variable 'b' would also be zero when its deserialized. In the explanation it says: The variable 'b' exists in the template, instead of an object (because its a static variable). Therefore the template value "5" will be printed by ob2.b.

Why does that mean? Yes its a static variable, it belongs to a class. But I dont see how that affects it value when its deserialized. I would think as static variables are not serialized, it would be reset to int's default value of zero. Can someone explain this to me?
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Values of static variables are not touched during serialization and deserialization...
 
Robert O'Leary
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Values of static variables are not touched during serialization and deserialization...



Thanks for your response, but that doesnt answer my question. Maybe I should re-phrase:

Static variables belong to class and not to an individual instance. The concept of serialization is concerned with the object's current state. Only data associated with a specific instance of a class is serialized, therefore static member fields are ignored during serialization. Ignoring to me would suggest that when you deserialize an object, its static variables would be set to their default value e.g zero for an int, null for object reference. So why is this not the case with the above example in mind?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert O'Leary wrote:This is Q25 from ExamLab SCJP5.



The output is "4 5 0".

I would (have) thought that the static variable 'b' would also be zero when its deserialized.

Why does that mean? Yes (it's) a static variable, it belongs to a class. But I (don't) see how that affects it value when (it's) deserialized. I would think as static variables are not serialized, it would be reset to int's default value of zero. Can someone explain this to me?



In this particular case, when you construct an instance of A before writing it out, A.b gets the value 5. When you read it back in, the deserialization doesn't alter the state of b, so it's still 5. If you actually wrote the serialized file from one JVM and read it in on another one (perhaps by using a command-line argument to switch between "read" mode and "write" mode), A.b would be 2 in the JVM that had only deserialized, but not constructed, objects of class A.
 
author
Posts: 23936
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for your response, but that doesnt answer my question. Maybe I should re-phrase



Well, first of all, yes, it does answer your question...

I think you are wondering... if "transient" variables are set to zero (because they are not serialized out, and hence, not read back, and have the default value of zero), why aren't static variables set to zero?

Remember, there is only one copy of a static variable. So, in this case, when you don't affect it, it keeps the current value. With transient variables (which are not static obviously), it is a new variable, so it gets the default value.

Henry

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes great explanations from you all, but the explanation in ExamLab 6 really confused me:

"The story of this program is to serialize an object of class A, to the file "A.ser" and create a new reference "ob2" by desterilizing [sic] the above saved object.

You have to know, any information about transient variables nor static variables may NOT add to the serialized file.

Therefore, after the deserialization, the only variable restored is 'a'. Because their [sic] are NO information about either 'b' nor 'c', in the serialized file.

However, the variable 'b' exists in the template, instead of an object (because it is a static variable). Therefore, the template value "5" will be printed by the expression "ob2.b". "



The most confusing statement is the second sentence about "... static variables may NOT add to the serialized file." It is not familiar Java jargon to me and so I don't know how that should be interpreted.
 
Henry Wong
author
Posts: 23936
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larry Chung wrote:
The most confusing statement is the second sentence about "... static variables may NOT add to the serialized file." It is not familiar Java jargon to me and so I don't know how that should be interpreted.



It is just saying that, by default, transient and static variables are not serialized.

Henry
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, duhhhh.

Thank you, Henry. I was completely confused when I tried to parse the ExamLab explanation but you succinctly made it crystal clear to me now.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic