Domanda

I have a simple singleton:

class Test1 implements Serializable {
    private static Test1 instance;

    public String a = "a";
    public String b = "b";
    public String c = null;
    public String d = null;
    public String e;
    public String f;

    private Test1() {
        e = "e";
    }

    public static Test1 getInstance() {
        if (instance == null) {
            instance = new Test1();
        }
        return instance;
    }

    // http://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-with-examples
    protected Object readResolve() {
        return getInstance();
    }

    public String toString() {
        return String.format("Test1{ a:%s, b:%s, c:%s, d:%s, e:%s, f:%s}", a, b, c, d, e, f);
    }
}

My main():

if ((new File("t1.obj").exists() == false)) {
    Test1 t1 = Test1.getInstance();
    t1.b = "bb";
    t1.d = "dd";
    t1.f = "ff";

    serialize("t1.obj", t1);
}

else {
    Test1 t2 = deserialize("t1.obj");
}

First run looks well

Serialized hu.fehergeri13.abptc.server.Test1 object to t1.obj file.
Test1{ a:a, b:bb, c:null, d:dd, e:e, f:ff}

after second run:

Deserialized hu.fehergeri13.abptc.server.Test1 object from t1.obj file.
Test1{ a:a, b:b, c:null, d:null, e:e, f:null}

My serialize/deserialize:

public static void serialize(String filePath, Object o) {
    try {
        FileOutputStream fos = new FileOutputStream(filePath);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(o);

        System.out.println(String.format("Serialized %s object to %s file.", o.getClass().getName(), filePath));
        oos.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static <T> T deserialize(String filePath) {
    try {
        FileInputStream is = new FileInputStream(filePath);
        ObjectInputStream ois = new ObjectInputStream(is);
        T o = (T) ois.readObject();

        System.out.println(String.format("Deserialized %s object from %s file.", o.getClass().getName(), filePath));
        ois.close();
        is.close();
        return o;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

Why are the values keep default and not bb,dd,ff?

È stato utile?

Soluzione

My serializable singleton:

public class MySingleton implements Serializable {

    private static final long serialVersionUID = -5909418239300111453L;
    private static MySingleton instance = null;

    protected MySingleton() {

    }

    public static MySingleton getInstance() {
        if (instance == null) {
            instance = new MySingleton();
        }
        return instance;
    }

    protected Object readResolve() {
        return getInstance();
    }

    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        ois.defaultReadObject();
        instance = this;
    }

    private String attr;

    public String getAttr() {
        return attr;
    }

    public void setAttr(String attr) {
        this.attr = attr;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top