import java.io.*;
import java.util.Vector;
import java.util.Enumeration;

class Body {
    private long idNum;

    public String name = "<unnamed>";
    public Body orbits = null;
    private static long nextID = 0;
    private static Vector bodies = new Vector();

    Body() {
	idNum = nextID++;
	bodies.addElement(this);
    }

    Body(DataInputStream in) throws IOException {
	idNum = nextID++;
	name = in.readUTF();
	String orbitsName = in.readUTF();
	Enumeration enum = bodies.elements();
	while (enum.hasMoreElements()) {
	    Body b = (Body)enum.nextElement();
	    if (orbitsName.equals(b.name)) {
		orbits = b;
		return;
	    }
	}
	throw new IOException(name + " orbits unknown body");
    }

    public void write(DataOutputStream out) throws IOException {
	out.writeUTF(name);
	out.writeUTF(orbits.name);	// identify body this orbits by name
    }
}

A static Vector object is used to keep track of Body objects that have been
created, so that they can be looked up by name when referenced in input
streams (for setting the "orbits" field).

Note that this implementation is simplistic in that it stores the reference
to the Body it orbits by name, and it depends on that Body being in memory
when this Body is being constructued from the stream.  Therefore, Body
objects must be written to a stream in a specific order, or they cannot
be read back in correctly (since the "orbits" field is public, this class
cannot perform lazy binding from the name of the orbited object to the
proper instance, either).  The Object Serialization mechanism available in
Java 1.1 can take care of most of these issues automatically.

