Using Serialization and the Serializable Fields API

Abstract

This example shows how to use the Serializable Fields API with Serialization, demonstrating that the class can define fields other than those already in the class to be serializable. This differs from just rewriting the writeObject method to customize the data format (see the Custom Data Format example) because, in this example, versioning support still holds.

Using the Serializable Fields API, this example specifically changes the internal representation of a rectangle from x1,y1,x2,y2 implementation (see OriginalClass.java) to Point(x1,y1), Point(x2,y2) (see EvolvedClass.java) while the external representation still remains x1, y1, x2, y2. This ensures bidirectional compatibility between the original and evolved representations.

As with all examples dealing with evolution, here the evolved class (ARectangle) must define a serialVersionUID field that is obtained using the command serialver on the original class (ARectangle).

blue bar

Additional Information

Source Files
Running the Example
List of Examples

Oracle and/or its affiliates Copyright © 1993, 2015, Oracle and/or its affiliates. All rights reserved.

微信小程序

微信扫一扫体验

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部