I've created a tree data structure that needs to be serializable, but every tree shares a common root object, which is a public static final constant.
The problem I'm having is illustrated below. After I serialize the ROOT object, deserialization creates a new Tree object instead of returning a reference to the original constant.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
public class Main {
public static final class Tree implements Serializable {
public final ArrayList<Tree> branches = new ArrayList<>();
}
public static final Tree ROOT = new Tree();
public static void main(String[] args) throws Exception {
File file = new File("C:/Users/Stephen/Desktop/temp.bin");
ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream(file));
oout.writeObject(ROOT);
oout.writeObject(ROOT);
oout.close();
ObjectInputStream oin = new ObjectInputStream(new FileInputStream(file));
Tree t1 = (Tree) oin.readObject();
Tree t2 = (Tree) oin.readObject();
oin.close();
System.out.println(ROOT == t1); // false
System.out.println(t1 == t2); // true
}
}
The first print statement outputs "false," but I want it to be "true."