@@ -40,15 +40,15 @@ private static void read() {
40
40
/**
41
41
* Records are serialized different from ordinary classes.
42
42
* Its fields are written in the same order as declared.
43
- * When it is read, the component constructor as called as normal code would need to do.
43
+ * When it is read, the component constructor is called as normal code would need to do.
44
44
* It cannot customize its serialization, only the method `writeReplace` can be used to
45
45
* create a copy to be serialized.
46
- *
46
+ *
47
47
* The value of serialVersionUID is fixed to 0L. If we added components to the record,
48
48
* it will contain the default value for that type.
49
49
* If we change the existing componentes it will throw something like:
50
50
* `java.io.InvalidClassException: CustomSerializableRecord; incompatible types for field x`
51
- *
51
+ *
52
52
* http://cr.openjdk.java.net/~chegar/records/spec/records-serialization.03.html#serialization-of-records
53
53
*/
54
54
record CustomSerializableRecord (int x , int y ) implements Serializable {
@@ -63,6 +63,11 @@ private Object writeReplace() throws ObjectStreamException {
63
63
return new CustomSerializableRecord (x + 1 , y + 1 );
64
64
}
65
65
66
+ private Object readResolve () throws ObjectStreamException {
67
+ System .out .printf ("Reading from serialized object - x: %s, y: %s\n " , x , y );
68
+ return new CustomSerializableRecord (x - 1 , y - 1 );
69
+ }
70
+
66
71
// Won't be called
67
72
private void writeObject (ObjectOutputStream out ) throws IOException {
68
73
System .out .println ("Writing object..." );
0 commit comments