Thursday, July 21, 2011

Can i serialize static variable in java ?

No you con't serialize static variable

Actually it working fine when you are going to serialize object it's contain static variables. but
A static variable is part of a class, not part of any one object. When you are going to serialize if your class contain static variables it leads to problems some time.

Consider this example
import java.io.*;    
public class Number implements Serializable
{
static int n1;
int n2;
public Number()
{
n1=10;
n2=15;
}
int number()
{
n2=20;
return n2;
}
public static void main(String[] ar)
{
Number n=new Number(); // constructor intialize values n1=10,n2=15
System.out.println("Before Serialization n1="+n.n1); //print n1=10;
System.out.println("Before Serialization n2="+n.number()); //here call the number() method on n so it set's n2 =20 and it return. and print.
try
{
FileOutputStream fos=new FileOutputStream("Number.txt");
ObjectOutputStream os=new ObjectOutputStream(fos);
os.writeObject(n); //when you are going to write n object it's values are n1 =10,n2=20
os.close();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
FileInputStream fis=new FileInputStream("Number.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
n=(Number)ois.readObject();
ois.close();
}
catch(Exception e)
{
e.printStackTrace();
}
//after retreving values. n1 = 10,n2 =20 only.
System.out.println("After Serialization n1="+n.n1);
System.out.println("After Serialization n2="+n.n2);
}
}

if you are class contain one static variable it is serialized. after that the value of static variable will be changed and again you are serialize the object. When you are going to deserialize of first object static variable value will not come before modified static value
so if you class including any static members, will already have been initialized.
with in this case you are write serialization and de-serialization in one class so you con't understand what happen actually.

Actually happening thing is this.
Am writing to programs if you are compile below 2 programs you can understand why static variables con't serialize

serialize the object
import java.io.*;  
class Serial
{
public static void main(String[] ar)
{
Number n=new Number();
System.out.println("Before Serialization n1="+n.n1);
System.out.println("Before Serialization n2="+n.n2);
try
{
FileOutputStream fos=new FileOutputStream("Number.txt");
ObjectOutputStream os=new ObjectOutputStream(fos);
os.writeObject(n);
os.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

de-serialization of object

import java.io.*;   
class Deserial {
public static void main(String[] args) {
Number n=null;
try {
FileInputStream fis=new FileInputStream("Number.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
n=(Number)ois.readObject();
ois.close();
}
catch(Exception e) { e.printStackTrace(); }
System.out.println("After Serialization n1="+n.n1);
System.out.println("After Serialization n2="+n.n2);
}
}


if you are going to serialize and de-serialize in one class. at the time of de-serialization already initialized static variable values will be printed if you are going to serialize and de-serialize in different class you know the truth.

No comments:

Post a Comment