• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Serializable statics

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K & B SCJP 5 page #493 "...remember that serialization is about instances, so static variables aren't serialized."

But that doesn't agree with this example. Can anyone shed light on this?

import java.io.*;
public class TestSer {
public static void main(String[] args) {
SpecialSerial s = new SpecialSerial();
try {
ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream("myFile"));
os.writeObject(s); os.close();
System.out.print(++s.z + " ");
ObjectInputStream is = new ObjectInputStream(
new FileInputStream("myFile"));
SpecialSerial s2 = (SpecialSerial)is.readObject();
is.close();
System.out.println(s2.y + " " + s2.z);
} catch (Exception x) {System.out.println("exc"); }
}
}
class p implements Serializable {}
class SpecialSerial extends p {
transient int y = 7;
static int z = 9;
}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think this example shows a static variable being written out.

You are access a static variable with a reference, so what you are printing is the static variable z associated with the SpecialSerial class which is still in memory.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chris,

I think your code clearly demonstrates that static vars are NOT part of the serialization process.

After serialization, you increment the static variable from 9 to 10. and print it.
Then you deserialize from disk into variable s2.
You print out y, which is transient, and so not deserialized into its original value 7, but 0.
And the last output is again variable z, that is still 10.

If it had been deserialized, it would be 9.


By the way, in both times you access the static variable through an instance, but if you did it through its class name (as it should be done) you see, why static variables cannot be part of the serialization process. Simply, BECAUSE they are static and not part of the object.


Perhaps change both the static and the nonstatic transient variable after serialization but before deserialization.

You will see, that in the DEserialized object, the transient var will change to 0 but the static is still at the value before deserialization.


Good luck,
Bu.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this example definatly shows static variable which are associated with class. and not being written to file.

actually it is a little tricky program, it seems that object and its instance variables are being written to file but real problem exist in the concept that static variables are associated with class and can be referanced by any referance variable of that class type + static variables can not be serialized.
 
Chris Judd
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, that is tricky. I understand you explanations. Thanks very much all.

-Chris
 
reply
    Bookmark Topic Watch Topic
  • New Topic