Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Serializable

i have made a serialized class,like this

`

class Example implements Serializable
{
    transient byte i=2;
    transient byte j=3;
    Example(){System.out.println("value of i:"+i+",j:"+j);}
}

`
when i am serilizing n deserializing the class,i.e.

    class SerialClass
{
public static void main(String []r)//throws IOException
{
try{
    System.out.println("Serialization");
    Example e=new Example();
    FileOutputStream out=new FileOutputStream("hyxa_code.txt");
/*File f=new File("copt.txt");
f.createNewFile();*/
    ObjectOutputStream oo=new ObjectOutputStream(out);
    oo.writeObject(e);
    oo.close();}catch(IOException e){}


try{
System.out.println("Deserialization");
    Example ee=new Example();
FileInputStream in=new FileInputStream("hyxa_code.txt");
ObjectInputStream o=new ObjectInputStream(in);
ee=(Example)o.readObject();
System.out.println("The vlaue of i,j:"+ee.i+" "+ee.j);
}catch(IOException e)
{}
catch(ClassNotFoundException e){}
}
}

the output is coming like this:

Serialization
value of i:2,j:3
Deserialization
value of i:2,j:3
The vlaue of i,j:0 0

but as i have heard, Deserialization doesn't initialize constructor, here is happening,why??? also,why the value of both variables is coming as it was initialized

like image 526
JavaResp Avatar asked Sep 14 '26 02:09

JavaResp


2 Answers

You're explicitly calling the constructor twice - once to serialize and then once when deserializing:

Example ee=new Example();
FileInputStream in=new FileInputStream("hyxa_code.txt");
ObjectInputStream o=new ObjectInputStream(in);
ee=(Example)o.readObject();
System.out.println("The vlaue of i,j:"+ee.i+" "+ee.j);

You're ignoring the value you've created though, so the code is effectively like this:

FileInputStream in=new FileInputStream("hyxa_code.txt");
ObjectInputStream o=new ObjectInputStream(in);
Example ee=(Example)o.readObject();
System.out.println("The vlaue of i,j:"+ee.i+" "+ee.j);

The difference is just that this time you won't be calling the constructor unnecessarily.

That explains why you're seeing the "value of i:2,j:3" line twice. The reason that the deserialized objects have values of 0 is because you've declared the variables as transient, as described in the other answers.

like image 57
Jon Skeet Avatar answered Sep 15 '26 17:09

Jon Skeet


Having the variables marked as transient keeps them from being serialized. Remove the transient part of the variable declaration and it will work.

And you are correct about the constructor not being invoked as part of deserialization. The state of the object is loaded directly from the stream and no constructor is invoked.

See http://www.rockstarprogrammer.org/post/2006/jul/29/more_you_want_know_about_java_serialization/ for more information. The readObject method can be used to initialize transient variables when deserialization happens.

like image 45
John Meagher Avatar answered Sep 15 '26 17:09

John Meagher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!