简体   繁体   中英

Serialization of Objects (java)

I have the following code:

import java.io.*;

public class TestSer {
    public static void main(String[] args) {
        SpecialSerial s = new SpecialSerial();
        try {
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("myFile"));
            os.writeObject(s); os.close();
            System.out.print(++s.z + " ");                         ...(1)
            ObjectInputStream is = new ObjectInputStream(new FileInputStream("myFile"));
            SpecialSerial s2 = (SpecialSerial)is.readObject();
            is.close();
            System.out.println(s2.y + " " + s2.z);
        } catch (Exception x) {
            System.out.println("exc");
        }
    }
}

class SpecialSerial implements Serializable {
    transient int y = 7;
    static int z = 9;
}

Now when i ran the code, the output was "10 0 10". But why is it "10 0 10" and not "10 0 0"? I mean when i deserialized the object, y and z (being transient and static resp) should have returned as 7 and 9, which are the default values (please correct me at this point cause i think that the values the object gets after the deserialization are default values). And what does the syntax "++object.var" means(refer (1)). Is it the same as "Object var++" ie, is "++sz" same as "s.z++"?

There's only one memory location that holds the value of SpecialSerial.z in memory since z is declared as static . This value takes precedence over the value deserialized from a file.

Adding:

private void writeObject(ObjectOutputStream oos) throws IOException {

    oos.defaultWriteObject();
    oos.writeInt(z);
    System.out.println("session serialized");
}

private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {

    ois.defaultReadObject();
    z = ois.readInt();
}

does overwrites the static value of z with the one deserialized from the file.

I wouldn't suggest this technique since it could cause problems hard to solve.

Now when i ran the code, the output was "10 0 10". But why is it "10 0 10" and not "10 0 0"?

When an object is obtained via deserialization, transient variables are filled with default values. But it shares the same static variable (or class variable) which is existing in JVM for the given class. That's why the value of sz is showing as 10 which is the existing value of z in the current application running.

is "++sz" same as "s.z++"?

++sz is using preincrement operator which means increase the value of sz first and then print it. Whereas, s.z++ is using postincrement operator which means print the value of sz first and then increment.

++sz means "add 1 to sz and then use it", s.z++ means "use sz and then add 1"... in order to answer your main question please answer this simple one: in your class "Special series" do you override the "isClose()" method? Can you put the code?

Static variables are associated with class. So, once you increment z from 9 to 10, it will remain 10 as long as the program is running. Also, the transient variable, y, does not get any value assigned during deserialization, and hence its value is 0 (default value of int variable).

Keep in mind static variables are at class level.

Object serialization is done on the object level. So serializing and deserializing doesn't have an impact on the static variable count.

Since y is transient it will be set to the default value which is 0 after the object is read.

System.out.print(++s.z + " "); 

Output is 10 because your object is in memory, serialization doesn't erase transient fields, it's just ignore them on serialization.

 System.out.println(s2.y + " " + s2.z);

Output is 0 and 10, 0 because y is transient field, it's not saved on bean serialization, so upon deserialization it gets default value, witch is 0 for int.

10 is because z is static field, thats mean it is not wired with instance, serialization also doesn't erase it.

It is equal:

s2.z and SpecialSerial.z
System.out.print(++s.z + " ");  

This will increase z by one and then print it out.

And because z is static it will be the same for all instances and therefore it will also be 10 after the deserialization. It is no longer 9 because you increased the value before the serialization.

y is 0 because its declared transient which means it will not be part of the serialized object. So no value will be set when doing the deserialization.

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