• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Serilazation

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read that static variables are not saved during serilazation ,but in the following code i belive that value of satic variable is saved.please help me.
class a
{
static int i=10;
public a()
{
i+=10;
}
public static void main(String []avg)
{
try{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream
a a1=new a();
oos.writeObject(a1);
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(("c:\\test\\a.set"));
a a2;
a2=ois.readObject();
System.out.pritnln(a2.i);
}catch(Exception e){System.out.println(e);}
}
}
i got output as: 20.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since because, the object's state is being deserialized in the same VM by then the class is still persisting in the JVM, its able to retain the value of the static variable, i think!

Perhaps, this thread will help you Sankar.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let's slide this back to the SCJP forum...
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even if you commnet the line to readObject the value of i
would still be 20.
so think whether i was persisted in the object?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a new info for me
"static variables are not saved during serilazation "
means is it just like a transient ?
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it is because its associated with the class instead of objects!
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Raghavan is right

Since because, the object's state is being deserialized in the same VM by then the class is still persisting in the JVM, its able to retain the value of the static variable, i think!



I had first saved the object and then i close my ecliplse and then I removed code for serialization.now my code is as follow. And i ran it and i got correct value for static varaiable i.e 10

try{
//ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(("a.set")));
//a a1=new a();
//System.out.println("a1 is initialized");
//oos.writeObject(a1);
//System.out.println(a1.i);
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(("a.set")));
a a2;
a2=(a)ois.readObject();
System.out.println(a2.i);
}catch(Exception e){System.out.println(e);}
 
Pravin Jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"static variables are not saved during serilazation "
means is it just like a transient ?


static variables are not like transient.
Since transient variables are part of the object and can be
persisted(saved) using the custom serialization which
can be provided for instances of a class by defining
a writeObject(ObjectOutput oo) method in a class.
 
It's a tiny ad only because the water is so cold.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic