jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes serialize a static variable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "serialize a static variable?" Watch "serialize a static variable?" New topic
Author

serialize a static variable?

Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
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 ]

G
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Search this forum - the question has been asked and answered more than once before.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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 ]
Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
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:
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

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


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

Joined: Feb 19, 2007
Posts: 121
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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: serialize a static variable?