Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static members are un-serializable??

 
Sharda Vajjhala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
maheshkumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
=================

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic