• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialization in K&B

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serialization is NOT on the exam as Bert stated, see my responses on Serialization for more insight:

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

 
Miltos Deligiannis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tommy Delson wrote:Serialization is NOT on the exam as Bert stated, see my responses on Serialization for more insight:

https://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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 125
Postgres Database BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 1
Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Diego Lemos wrote:Hi Miltos,
Achilleas is right.



Of course! That's the most plausible thing to happen
 
It runs on an internal combustion engine. This ad does not:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic