2

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 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If you want to have more control over the serialization, for example if the class you are serializing has transient values that you need to have serialized, but you cannot change the source, consider using an alternative Java serializer, for example, like json-io (github.com/jdereg/json-io)
4

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.

3 Comments

that's true..but why constructor is being initialized again,why is it so
JAVAFREAK: See my answer. You're explicitly calling it twice!
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.
2

Transient variables can't be serialized hence the value of variables became 0.

Here you have an example : http://javatechnologyhelper.blogspot.com/2014/04/java-serialization.html

Also, you can take a look at: http://en.wikibooks.org/wiki/Java_Programming/Keywords/transient.

transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes. Those bytes are sent over the network and the object is recreated from those bytes. Member variables marked by the java transient keyword are not transferred, they are lost intentionally.

Comments

1

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.

1 Comment

but the constructor is showing there default value which is assigned before Serialization

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.