import java.io.*; public class Main implements Serializable { private int width; private int size; public void setWidth(int width) { this.width = width; } public void setSize(int size) { this.size = size; } public static void main(String[] args) { Main m = new Main(); m.setWidth(22); m.setSize(84); try { FileOutputStream fileOut = new FileOutputStream("andriy.ser"); ObjectOutputStream objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(objectOut); objectOut.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } } 

UPD. Missed that it was necessary to transfer the link to a copy of the class Main

ObjectOutputStream does not implement Serializable, so probably. In general, the string objectOut.writeObject (objectOut); looks weird. - zRrr

Eclipse screenshot

    1 answer 1

    objectOut.writeObject(objectOut);

    Do you want an object to record serialized objects to record itself? More formally, then:

    Only objects that support the java.io.Serializable interface can be written to streams

    The ObjectOutputStream class ObjectOutputStream not implement the java.io.Serializable interface, hence the exception.

    Most likely you wanted to write:

    objectOut.writeObject(m);