Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Serilazation

  Asked By: Conrad    Date: Dec 28    Category: Java    Views: 466
  

I read that static variables are not saved during serilazation ,but
in the following code i belive that value of satic variable is
saved.please help me.


class a
{
static int i=10;
public a()
{
i+=10;
}
public static void main(String []avg)
{
try{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream
a a1=new a();
oos.writeObject(a1);
ObjectInputStream ois=new ObjectInputStream(new FileInputStream
(("c:\\test\\a.set"));
a a2;
a2=ois.readObject();
System.out.pritnln(a2.i);
}catch(Exception e){System.out.println(e);}
}
}

i got output as: 20.

Share: 

 

8 Answers Found

 
Answer #1    Answered By: Utsav Shah     Answered On: Dec 28

problem is in your empty-argument constructor. when you serialize the object, '10' is stored for 'i'. but when deserialized it automatically calls the empty argument constructor to rebuild the object. thats why you get value '20'. add another non-empty-constructor as follows:
public a(int y){
i+=10;
}
and modify your empty constrctor
public a(){}

 
Answer #2    Answered By: Ziza Mizrachi     Answered On: Dec 28

the point is once the object is read  the static  field is initialized any way,

by having
a a2;

your static member variable  is initialized no matter if it has been serialized , saved, etc

so once the add method is called it will result 20 in any circumstances

instead of adding it up, print it and you'll see

 
Answer #3    Answered By: Fairuzah Alam     Answered On: Dec 28

You are 'accessing a static  member via instance' which is not a
correct programming habit. Compiler will translate 'a2.i' to 'a.i' and
that's the reason of your surprise

 
Answer #4    Answered By: Gerardo Morgan     Answered On: Dec 28

when the object is resored from file(deserialiazed) it automatically calls the empty-argument constructor that adds a '10' to default value.

 
Answer #5    Answered By: Kawakib Mansour     Answered On: Dec 28

read  it too fast, I thought you have an add method. the answer is the same though
you are addign 10 in the constructor. so again the answer is the same, the object is init 10 to statis and adding 10 in constructor.
the job is done by a a2; and not by reading from the stream

 
Answer #6    Answered By: Julia Hughes     Answered On: Dec 28

As far as I know if you want to exclude a class  field while serializing you have to use modifier "transient". Keyword "static" has no effect on serialization.
Please let me know if there is any reliable source suggesting that "static" modifier affects serialization.

 
Answer #7    Answered By: Scarlett Hughes     Answered On: Dec 28

See this like:www.beginner-java-tutorial.com/...rialization.html

Java does not serialize or deserialize transient or static  fields.

 
Answer #8    Answered By: Marina Smith     Answered On: Dec 28
 
Didn't find what you were looking for? Find more on Serilazation Or get search suggestion and latest updates.

Related Topics:



Tagged:  

 
 
 

Related Post