简体   繁体   中英

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,ie

    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

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM