Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static variable - serialization Q  RSS feed

 
Robert O'Leary
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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?
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
David Lee Lambert
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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

 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!