Reputation: 1
I have:
public class Mouse implements Serializable {
int i=100,j=200;
static int k=100;
public static void main(String[] args) throws IOException {
Mouse m=new Mouse();
FileOutputStream fos=new FileOutputStream("E:\\santosh.txt");
ObjectOutputStream os=new ObjectOutputStream(fos);
os.writeObject(m);
os.flush();
System.out.println("success");
os.writeObject(m);
os.flush();
}
and:
public class Cat {
public static void main(String[] args) throws IOException, ClassNotFoundException {
FileInputStream fis=new FileInputStream("E:\\santosh.txt");
ObjectInputStream oi=new ObjectInputStream(fis);
Mouse m=(Mouse) oi.readObject();
System.out.println("i="+m.i+" j="+m.j);
System.out.println("k="+m.k );
}
}
I am getting output as i=100 j=200 k=100 . How this is possible that static variable is accessed.
Upvotes: 0
Views: 32
Reputation: 310860
It isn't serialized at all, and therefore it isn't accessed while it is serialized either. It value is undisturbed at the receiver. It declared as initially 100 and that is what you saw.
Upvotes: 1