• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

static members are un-serializable??

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
=================

 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic