• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

serialization

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

class TestSer
{
public static void main(String[] m)
{
SpecialSerial s=new SpecialSerial();
try{ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("myFile"));
os.writeObject(s);
os.close();
System.out.println(++s.z + " ");
ObjectInputStream is=new ObjectInputStream(new FileInputStream("myFile");
SpecialSerial s2=(SpecialSerial)is.readObject();
is.close();
System.out.println(s2.y+ " "+s2.z);
}catch(Exception x){System.out.println("exec");}
}
}
}
class SpecialSerial implements Serializable
{
transient int y=7;
static int z=9;

}

output is:10 0 10 how as we know transient and static dont serialize therefore i think it should be 10 0 0


}
}
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that the static member is not being serialized. The deserialized object doesn't have a "z" value. You can see it debugging the code.

But, since "z" is static and belongs to the class (not to the instances), its value is always there regardless new instances of the class;

Am I right?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right Rafael, static variable is not being written on file, not being read from file, it will not loose its value as it is class variable, it will not depend on instance objects being serialized here.
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic