[Solved] Exception in thread “main” java.lang.ClassCastException [duplicate]


At this line:

oos.writeObject("value of  i is" + r.i);

You serialize the String "value of i is9" literally instead of the object itself. Thus the only possible cast is to the String again.

String string = (String) f9.readObject();

To fix this issue, serialize the whole object:

rak r = new rak();
r.i = 9;
// ...
oos.writeObject(r);

And the result of the last line would be correct:

// ...
rak r1 =  (rak) f9.readObject();
System.out.println(r1.i);          // prints 9

4

solved Exception in thread “main” java.lang.ClassCastException [duplicate]