File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serializable statics 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 "Serializable statics" Watch "Serializable statics" New topic

Serializable statics

Chris Judd

Joined: Feb 25, 2007
Posts: 23
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?

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();
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;
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
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.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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,

all events occur in real time
saqib sarwar
Ranch Hand

Joined: Mar 30, 2007
Posts: 77
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.

SCJP5 95%, SCWCD 85%<br />Knowledge is the Life of Mind
Chris Judd

Joined: Feb 25, 2007
Posts: 23
Wow, that is tricky. I understand you explanations. Thanks very much all.

I agree. Here's the link:
subject: Serializable statics
It's not a secret anymore!