views:

179

answers:

3
+1  Q: 

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

+4  A: 

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.

John Meagher
that's true..but why constructor is being initialized again,why is it so
JavaResp
JAVAFREAK: See my answer. You're explicitly calling it twice!
Jon Skeet
The constructor isn't being invoked for the deserialization. The other constructor call is from the line just below the println("Deserialization") call where another new Example is being created.
John Meagher
Jone Meagher :thanks to u also
JavaResp
+1  A: 

As both i and j are marked as transient they will not be serialized. So when you deserialize they will have the default value for byte, which is 0.

Kees Kist
but the constructor is showing there default value which is assigned before Serialization
JavaResp
+3  A: 

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.

Jon Skeet
thanks ,i have understood the problem in the code
JavaResp