This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static variable - serialization Q Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Static variable - serialization Q" Watch "Static variable - serialization Q" New topic
Author

Static variable - serialization Q

Robert O'Leary
Ranch Hand

Joined: Mar 24, 2009
Posts: 41
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?


SCJP6
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Values of static variables are not touched during serialization and deserialization...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Robert O'Leary
Ranch Hand

Joined: Mar 24, 2009
Posts: 41
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

Joined: Oct 02, 2006
Posts: 7
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

Joined: Sep 28, 2004
Posts: 16690
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 245
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.

SCJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16690
    
  19

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

Joined: Feb 02, 2010
Posts: 245
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Static variable - serialization Q
 
Similar Threads
I am confused
static variables serialization
static variable and Serialization.
Serialization
About object ,reference and class variable