*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static members are un-serializable?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static members are un-serializable??" Watch "static members are un-serializable??" New topic
Author

static members are un-serializable??

Sharda Vajjhala
Ranch Hand

Joined: Nov 14, 2001
Posts: 57
Question from a mock exam:
1) Class fields with the following modifiers will not be serialized
private
static
transient
protected
Answer provided : static, transient.
Why static? I thought it was only transient. Is it because a static member belongs to the class and not to an instance and serialization is always done on objects??
Thanks
Sharda
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Yes, yes and Yes
But I have a question of my own:
What if Class object itself is serialized. Would it not save the static members?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Imagine you have two objects of the same class that share a static variable. You serialize one of the objects. You then change the contents of the static variable. Now you deserialize the first object. What should the contents of the static variable be?
The point is that static variables are associated with classes and not objects. But we serialize objects and not classes.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
What if I wish to stop the execution of application completely today. And I want all objects to be saved as they are. Also all static variables to be saved as they are today. Tomorrow, I want to resume the application from same status.
Would that require serializing static variables via Class object?
Thanks
Barkat
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
hi all, im preparing for sjcp2..
so wat is the right answer for the above mentioned question ??
is it only transient or both static and transient ???
Thanks,
raj


SCJP, SCWCD, SCBCD, Oracle Certified Professional (SQL n PL/SQL)
maheshkumar
Greenhorn

Joined: Aug 19, 2002
Posts: 2
import java.io.*;
public class test implements java.io.Serializable{
static int i;
int j;
public static void main(String args[]) throws Exception{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("txtfile")));
test t1 = new test();
t1.i=5;
t1.j=10;
out.writeObject((Object)t1);
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("txtfile")));
//t1.i=2;
//t1.j=5;
test t = (test)in.readObject();
System.out.println(t.i);
System.out.println(t.j);
in.close();
}
}
Output: 5
10
If u remove comments and execute
Output: 2
10
This means static field are serializable
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
From API ObjectInputStream:
The default deserialization mechanism for objects restores the contents of each field to the value and type it had when it was written. Fields declared as transient or static are ignored by the deserialization process.

From API ObjectOutputStream:
The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields.

So maybe what you see is not what you get?
-Barry
[ September 27, 2002: Message edited by: Barry Gaunt ]

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

Joined: May 05, 2000
Posts: 13974
Actually your code example proves that statics are not serializable.

If the static was serializable then the output would be 5 & 10 since that is what the variables looked like when the object was serialized. But the result is 2 & 10. The non-static variable is replaced and the static variable maintains the value it was changed to after the object was serialized.
[ September 27, 2002: Message edited by: Thomas Paul ]
[ September 27, 2002: Message edited by: Thomas Paul ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Barkat Mardhani:
What if I wish to stop the execution of application completely today. And I want all objects to be saved as they are. Also all static variables to be saved as they are today.
Store them in a properties file.
venu vasireddy
Greenhorn

Joined: Nov 15, 2005
Posts: 1
I came across this old post; which I stumped into same question. Yes both static and transient fields are not serialized.

Test is - Serialize object. Stop JVM ; and run DeSerialize test; you see null values for static fields and transient field objects;

public class Test1 implements Serializable {
static String s;
}

Program 1

public class Program1{
public static void main(String[] args) throws Exception {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:\\Desktop\\venu\\serial.test"));
Test1 t = new Test1();
t.s="Hello";
out.writeObject(t);
}
}

STOP JVM

Run this piece of code.
public class Program1{
public static void main(String[] args) throws Exception {
System.out.println(Test1.s); //before deserialization
ObjectInputStream in = new ObjectInputStream(new FileInputStream("C:\\Desktop\\venu\\serial.test"));
Test1 t = (Test1)in.readObject();
System.out.println(Test1.s); //After deserialization
}
}

========== string s value ====
null
null
=================

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: static members are un-serializable??
 
Similar Threads
valid combinations of Modifiers like static,native,transient etc
Serialization of static fields
transient modifier - Q25 of Kumar Majji
What restricts fields from Serialization
Static Final Transient