Where does the serialization code for the car go? Into Car class or into corObj object?
Assuming you're using an ObjectOutputStream
, the serialized data is streamed to whatever the ObjectOutoutStream
is wrapping. It could be to your local storage using a FileInputStream
to stream the data to a file on your drive:
File file = new File("car.obj");
FileOutputStream fileOut = new FileOutputStream(file);
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
objOut.writeObject(new Car()); //write
objOut.close();
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream objIn = new ObjectInputStream(fileIn);
Car car = (Car) objIn.readObject();
Where the serialized data goes depends on what you pass to the constructor of ObjectOutputStream
.
Above shows writing/reading to/from local storage. You can use a ByteArrayOutputStream
to keep the data in memory, or a stream from a Socket
to send it over a network.
After 3 cars have been serialized how can the 3 cars can be deserialized?
You must keep track of this yourself. Once the serialized data leaves the stream, it's virtually no longer in your application (unless you stream it to memory).
The point of streams is for I/O (input/output) - you are sending the data somewhere via a connection.
It could be kept inside your application by writing to a ByteArrayOutputStream
:
ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(arrayOut);
//write object
byte[] data = arrayOut.toByteArray(); //must keep a reference to this data! Contains serialized object
ByteArrayInputStream arrayIn = new ByteArrayInputStream(data);
ObjectInputStream objIn = new ObjectInputStream(arrayIn);
//read object
Or you can stream it out of your program via FileOutputStream
or an output stream from Socket
.
File file = new File("car.obj"); //must keep a reference to this file! Specified where serialized object is
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(file));
//write object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
//read object
corObj object keeps the collection of references to each created car instance. So after deserialization how can corObj refer again to those reconstructed car instances?
You must keep track of where you are streaming those objects to. If streaming to local storage, you must keep track of the file names. If streaming to a ByteArrayOutputStream
, you must keep a reference of that byte stream.
Would the previously created collection of car instances automatically refer back to reconstructed cars
No. By deserializing, you are creating a new object. If you serialize an object in a collection, then deserialize the object without ever removing the one from the collection, you now have 2 objects.
I highly recommend reading my answer on What is the penalty for unnecessarily implementing Serializable?. If you serialize an object, the modify the class of that object before deserializing the object, attempting to deserialize that object may fail as the binary data will no longer match.