Hi Suren,

Good Question asked. The static variable that you see is part of the
class initialization during deserialization.Its not part of the object
in any ways,so forget about it getting serialized.So if you modify
this static variable in any ways ,its not gonna reflect in the
deserialized state.It will give you the value with which it was
initailzed during class initialization.

For more light on this read the following FAQ from Java Ranch :

http://www.coderanch.com/t/277467/Streams/java/Serialization-static-variables


Thanks,
Ashok A V
On Sat, Dec 26, 2009 at 6:21 PM, suren <[email protected]> wrote:
> package SJCPTest;
>
> import java.io.FileInputStream;
> import java.io.FileNotFoundException;
> import java.io.FileOutputStream;
> import java.io.IOException;
> import java.io.ObjectInputStream;
> import java.io.ObjectOutputStream;
> import java.io.Serializable;
>
> public class SerialTest {
> public static void main(String a[])
> {
>        Dog ob=new Dog(50,"Puppy");
>        ob.change();
>        try {
>                FileOutputStream fo=new FileOutputStream("obj.ser");
>                ObjectOutputStream oo=new ObjectOutputStream(fo);
>                oo.writeObject(ob);
>        } catch (FileNotFoundException e) {
>                // TODO Auto-generated catch block
>                e.printStackTrace();
>        } catch (IOException e) {
>                // TODO Auto-generated catch block
>                e.printStackTrace();
>        }
>        try {
>                FileInputStream fi=new FileInputStream("obj.ser");
>                ObjectInputStream oi=new ObjectInputStream(fi);
>                Dog newob=(Dog)oi.readObject();
>                System.out.println("Weight:"+newob.weight);
>                System.out.println("Name:"+newob.name);
>                System.out.println("Static Variable tmp:"+newob.tmp);
>        } catch (FileNotFoundException e) {
>                // TODO Auto-generated catch block
>                e.printStackTrace();
>        } catch (IOException e) {
>                // TODO Auto-generated catch block
>                e.printStackTrace();
>        } catch (ClassNotFoundException e) {
>                // TODO Auto-generated catch block
>                e.printStackTrace();
>        }
>
>
> }
> }
> class Animal
> {
>        int weight;
>        Animal()
>        {
>                this.weight=100;
>        }
>
> }
> class Dog extends Animal implements Serializable
> {
>        String name;
>        static int tmp;
>        Dog(int weight,String name)
>        {
>                //super(weight);
>                this.weight=weight;
>                this.name=name;
>                tmp=77;
>
>        }
>        public void change()
>        {
>                weight=100;
>        }
> }
>
>
>
>
> This code results
>
> Weight:100
> Name:Puppy
> Static Variable tmp:77
>
> I don't know how it results tmp as 77?
> Somebody make me clear about this..
>
> --
> To post to this group, send email to 
> [email protected]
> To unsubscribe from this group, send email to 
> [email protected]
> For more options, visit this group at 
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en



-- 
Victory belongs to the most persevering.
 - Napoleon

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to