Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization in K&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization in K&B" Watch "Serialization in K&B" New topic
Author

Serialization in K&B

Miltos Deligiannis
Greenhorn

Joined: Jun 12, 2011
Posts: 29
From K&B: Serialization Is Not for Statics

And what
happens if you deserialize three different Dog instances, all of which were serialized
at different times, and all of which were saved when the value of a static variable
in class Dog was different. Which instance would "win"? Which instance's static
value would be used to replace the one currently in the one and only Dog class that's
currently loaded? See the problem?


I see the problem but what would be the answer? Ok statics are not serialized but what value do they take when an object is deserialized and needs to have these vars too?

Maybe i'm burned out from studying but do i miss the answer?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

If you want to restore the value of a static variable between sessions, you are responsible for saving the value yourself, and loading it when the class is initialized.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Serialization is NOT on the exam as Bert stated, see my responses on Serialization for more insight:

http://www.coderanch.com/t/539469/java-programmer-SCJP/certification/Serialization#2448680


OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Miltos Deligiannis
Greenhorn

Joined: Jun 12, 2011
Posts: 29
Tommy Delson wrote:Serialization is NOT on the exam as Bert stated, see my responses on Serialization for more insight:

http://www.coderanch.com/t/539469/java-programmer-SCJP/certification/Serialization#2448680



That's good news, but it's good to be cleared though...
Miltos Deligiannis
Greenhorn

Joined: Jun 12, 2011
Posts: 29
Stephan van Hulst wrote:If you want to restore the value of a static variable between sessions, you are responsible for saving the value yourself, and loading it when the class is initialized.


Ok so, for each of the 3 Dog objects you save manually each one's value for the static var, right? But when deserialized at the same time only one Class dog is loaded whose static var needs a value. which of the 3 (one per object)?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

No, you save the static variable when the session ends, not when you serialize an instance of the enclosing class.

This situation is rare though. I wouldn't worry about it too much. Static variables should rarely be allowed to be modified; and if they are, they usually represent temporary data that can be discarded between sessions.
If you find that you need to save a static variable, you've probably made a poor application design, and need to look at better ways to distribute responsibilities among your classes.

Bells should actually be ringing when you find yourself implementing the Serializable interface. Don't do it, unless you have really good reasons to.
Achilleas Achix
Ranch Hand

Joined: Apr 18, 2011
Posts: 123

Miltos Deligiannis wrote:
Stephan van Hulst wrote:If you want to restore the value of a static variable between sessions, you are responsible for saving the value yourself, and loading it when the class is initialized.


Ok so, for each of the 3 Dog objects you save manually each one's value for the static var, right? But when deserialized at the same time only one Class dog is loaded whose static var needs a value. which of the 3 (one per object)?


Milto, γιά σου και καλή τύχη με τις εξετάσεις

To your question, the static var apparently will take the value of the last object deserialized.


OCPJP 6.0
Diego Lemos
Greenhorn

Joined: Feb 18, 2011
Posts: 1

Hi Miltos,

Achilleas is right.

Look at this code :



The output is :

dog1 age before : 1
dog2 age before : 1
dog1 age after : 2
dog2 age after : 2


I hope this will help


Martin Jedrzejewski
Greenhorn

Joined: May 14, 2007
Posts: 11
I can imagine that such a static variables can actually be of use in various situations, ie.:
- count number of instances of class Dog
- add some of the Dog instance variables, like age, height etc., maybe count average
- maybe your Dog class is a singleton ( :) ), then static variable would be used to keep reference to it

each time this static variable should be calculated during deserialization time, I dont think there are good reasons to serialize static variables - because then it is a sign of bad design, maybe in case when Your class is a singleton.
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
In reference to the original post, I would imagine if you see fit that you make a specialized method to serialize the static variable. Serializing it in response to any-object-of-the-class being serialized doesn't make much sense since in that case the static variable would be directly connected to that specific object, but in fact its a variable shared by all objects of the class.

Just my opinion and not sure it was very clear, but maybe you get what I mean.

// Andreas
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Besides constants that are defined at class initialization, static fields usually represent transient data, as Martin implied.

There should never be a reason to commit static variables to a persistent storage.
Miltos Deligiannis
Greenhorn

Joined: Jun 12, 2011
Posts: 29
Diego Lemos wrote:Hi Miltos,
Achilleas is right.


Of course! That's the most plausible thing to happen
 
wood burning stoves
 
subject: Serialization in K&B