Write now I have this class that I would like to be able to save and open using serialization:
public class Region implements Serializable
{
private final int inputNumberOfColumnsAlongXAxis;
private final int inputNumberOfColumnsAlongYAxis;
private double inputDataScaleReductionOnXAxis;
private double inputDataScaleReductionOnYAxis;
private int numberOfColumnsAlongXAxis;
private int numberOfColumnsAlongYAxis;
private int cellsPerColumn; // Z-Axis dimension
private float inhibitionRadius;
private final float percentMinimumOverlapScore;
private final float minimumOverlapScore;
I’ve never done object serialization before, so any help would be greatly appreciated!
The simplest thing you need to do is add a private static field to your class with the name
serialVersionUID. For instance:This is used by the default serialization mechanism to match up class names and formats.
You can then write instances of your object to an
ObjectOutputStreamand read them back from anObjectInputStream:For more control over your object serialization, you can implement these methods:
You then have total control over the serialization of your objects. For more info, see the docs on
Serializable.UPDATE: Strictly speaking, you don’t need to declare
serialVersionUID; the runtime environment will calculate one for you automatically if it’s missing. However, the docs have this to say about it (emphasis in the original):