Create input and output streams. Use the input and output streams to create object input and object output streams. Pass the object that you want to copy to the object output stream. Read the new object from the object input stream and cast it back to the class of the object you sent. I have written a class called ObjectCloner that implements steps two through five.
Line D ensures the whole object has been sent.
The code on line E then creates a ByteArrayInputStream and populates it with the contents of the ByteArrayOutputStream . Of course, if you extend a class that is serializable, then the new class is also serializable. If there is an object whose class is not serializable, then a java.io.NotSerializableException will be thrown, telling you which class caused the problem. A quick implementation example is shown below. This object is then printed out to show its contents.