• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

serialization

 
vini singh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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


}
}
 
Rafael Angarita
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • 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?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic