---
title: "Java Enumerations: Serializing enum Types"  
description: "Earlier we learned that objects of the enum type can be serialized and compared to each other."  
author: "Simond Gear"  
published: 2016-05-26  
updated: 2019-11-24  
canonical: https://www.mindstick.com/articles/12132/java-enumerations-serializing-enum-types  
category: "java"  
tags: ["java"]  
reading_time: 4 minutes  

---

# Java Enumerations: Serializing enum Types

Earlier we learned that objects of the [enum](https://www.mindstick.com/Articles/12128/java-enumerations-why-we-need-enums) type can be serialized and compared to each other. Suppose we create an enumeration for the colors used in a drawing program. Such an enumeration may look like this:

```
enum ColorPalette {
RED, GREEN, BLUE        
}                                   
```

We can select the color green for the current drawing with the following declaration:

ColorPalette drawingColor = ColorPalette.GREEN;

We can now save the **drawingColor** object to a setting file by calling the **writeObject** method of the ObjectOutputStream class . The code might look like this:

```
ObjectOutputStream outStream = new ObjectOutputStream(
                        new FileOutputStream("Settings.dat"));
outStream.writeObject(drawingColor);
```

Later on, to read the settings from the Settings.dat file, we would use the following code:

```
ObjectInputStream inStream = new ObjectInputStream(                                                                                       new FileInputStream("Settings.dat"));
System.out.println("Retrieved object: "
+ (ColorPalette) inStream.readObject());
```

\

This prints **GREEN** to the terminal. Note that the message printed to the console is the string name of the constant rather than its ordinal value. The default implementation of the toString method of the enum class does this for us.

Earlier we learned that serialization withstands arbitrary changes in the **enum** type. So now, let’s modify our definition of the ColorPalette enumeration as follows:

```
enum ColorPalette {
RED, YELLOW, GREEN, MAGENTA, BLUE, VIOLET
}
```

\

Note that we have now added a new color (yellow) before green. If we read the previous **Settings.dat** file, GREEN is still printed to the console, although the ordinal value of GREEN has now changed. The trivial code for testing this is provided here:

```
import java.io.*;
public class EnumSerialization {
                     public static void main(String[] args) {
                             ColorPalette drawingColor = ColorPalette.GREEN;
                             try {
                                      System.out.println("Saving color setting");
                                      ObjectOutputStream outStream = new ObjectOutputStream(
                                                          new FileOutputStream("Settings.dat"));
                                      outStream.writeObject(drawingColor);
                                      outStream.close();
                                      ObjectInputStream inStream = new ObjectInputStream(
                                                          new FileInputStream("Settings.dat"));
                                      System.out.println("Retrieved object: "
                                                          + (ColorPalette) inStream.readObject());
                                      inStream.close();
                             } catch (IOException e) {
                                      System.out.println("Error reading/writing object");
                             } catch (ClassNotFoundException cfe) {
                                      System.out.println("Class casting error");
                             }
                     }
}
enum ColorPalette {
                     RED, GREEN, BLUE
}
```

---

Original Source: https://www.mindstick.com/articles/12132/java-enumerations-serializing-enum-types

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
