Forums Register Login

serialize a static variable?

+Pie Number of slices to send: Send
The following from K&B, page 520:


Now this may be perfectly obvious to all of you out there, but for me, my brain is slowing down I believe as a result of several months of cramming for this exam. The question asks what happens when you run this code. One of the correct answers is that it displays "10 0 10". Now the book (and I think Whizlabs) says that static variables are NOT serialized. OK, how is it that when object s2 in the above code is deserialized the static variable z comes back with the same value it was set at before its corresponding object s was serialized? If static variables are not serialized shouldn't this variable be "0" after deserialization?

As I said, this may be obvious but its zooming right over my head. Please help the slow-in-mind.
Thank you

[ June 14, 2007: Message edited by: G Marshall ]
[ June 14, 2007: Message edited by: G Marshall ]
+Pie Number of slices to send: Send
Search this forum - the question has been asked and answered more than once before.
+Pie Number of slices to send: Send
ok, I did as you stated. I searched the topic and found several answers. What I have uncovered when it comes to sereialization of static variables is this:
1. Static variables are NOT serialized
2. If your object is serialized and deserialized on(in?) the same JVM,
your static variable will have the same value when it is deserialized
as when it was serialized.
3. If your object is serialized and deserialized on(in?) a different
JVM, then your static variable will have its initial value - that
being the value it gets when the object is first created.
4. K&B may want to elaborate on this in their next edition.
+Pie Number of slices to send: Send
 

Originally posted by G Marshall:

1. Static variables are NOT serialized
2. If your object is serialized and deserialized on(in?) the same JVM,
your static variable will have the same value when it is deserialized
as when it was serialized.

#1 in that list is correct. But #2 ignores that and continues to talk as though serialization and deserialization had some effect on static variables. Here's a better way to state #2:

2. Serialization and deserialization have no effect on static variables.

Okay, now that looks pretty much the same as #1, so you could really get rid of #1 and have my version of #2 as your only rule.

Note that you don't need any special rules about same JVM or different JVM. Static variables aren't affected, so they retain whatever values they had before. That's all. You're making it way too complicated.
[ June 14, 2007: Message edited by: Paul Clapham ]
+Pie Number of slices to send: Send
Paul:
Thank you for uncomplicating this. OK. So I have a static integer variable, whose initial value = 9, that I set to 200 before serializing the instance of the class. Upon deserialization that same static varialble remains at 200, not its inital value of 9. Now with all due respect to you as you are the java guru and I am just a java grasshopper, this sure looks like the static variable is getting serialized!!

:roll:
+Pie Number of slices to send: Send
Here's a non-programming model that might clarify the situation for you.

On my desk there is a yellow piece of paper with the number "200" written on it. There are a lot of other pieces of paper there, too, with various things written on them.

You come into my office and take away several of the other pieces of paper. After a while you come back and put them back on my desk.

Now, on the yellow piece of paper is still written the number "200". Do you therefore conclude that must be because you took the yellow piece of paper away and brought it back?
+Pie Number of slices to send: Send
 

Originally posted by G Marshall:
Paul:
Thank you for uncomplicating this. OK. So I have a static integer variable, whose initial value = 9, that I set to 200 before serializing the instance of the class. Upon deserialization that same static varialble remains at 200, not its inital value of 9. Now with all due respect to you as you are the java guru and I am just a java grasshopper, this sure looks like the static variable is getting serialized!!

:roll:




How about change the test to something like this? Set the static integer variable, whose initial value = 9, to 200 *after* serializing the instance of the class.

Upon deserialization, is it the value of 9, which was the value you had when you serialized it? Or is it still 200, the value *after* you serialized it?

Henry
+Pie Number of slices to send: Send
 

Originally posted by Henry Wong:



How about change the test to something like this? Set the static integer variable, whose initial value = 9, to 200 *after* serializing the instance of the class.

Upon deserialization, is it the value of 9, which was the value you had when you serialized it? Or is it still 200, the value *after* you serialized it?

Henry



Henry:
Well lets see. Static variables are not serialized right? If I am understanding this chain of posts as well as others I have searched, then the static variable will be the same after serialization as it was before serialization. Therefore, given your scenario, I would expect the value of the static integer to be 200 after serialization.

If I am correct then the lesson here is that the class variables do not get touched during serialization and deserialization. Only the instance variables get serialized. Thats fine, but how does this work in a distributed application? If an object serializes itself on computer A and sends the serialized file (what was the file extension again?) to computer B and the object then deserializes on computer B, how do the class variables know what their values are? In our scenario how does the static variable in the object on computer B know that it should have a value of 200?

Or am I creeping out of the SCJP scope?
+Pie Number of slices to send: Send
 

In our scenario how does the static variable in the object on computer B know that it should have a value of 200?



It doesn't. And it may not be.

Whatever value that the static variable had on computer B, prior to the deserialization, will be its value after the deserialization.

Henry
[ June 14, 2007: Message edited by: Henry Wong ]
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1985 times.
Similar Threads
Serialization SCJP KS && BB
Question about Serialization
Serializing static variables
Serialization
question about serializable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:21:27.