Java Enum Constants Immutability Overview




Source Images - Pixabay

What are Enums?

Enums or Enumerations are special Classes that can create only a limited number of predefined objects. This is useful in the instances where you know exactly how many objects are needed to be available. Attempting to create an object which is not defined inside the Enum Class would cause an error. So, the object limitation is always guaranteed!

Creating an Enum Class

For creating a new Enum, select File → New → Enum in Eclipse and give it an appropriate name.

This Enum Class has any feature available in an ordinary Class. You can add variables and methods to it. It can have a main() method as well.

The only extra thing here is: you have to specify the names of the objects that you need in the beginning of the Enum, that is, below the Enum declaration sentence.

For example, suppose I want to create an Enum for the four seasons.

There are four objects in this Enum called SPRING, SUMMER, AUTUMN and WINTER. These objects are called as “Enum constants” because, they remain unchanged when the Enum is used in another Class.

The list of Enum objects ends with the semicolon. After that semicolon, you can add the variables and methods to your Enum.

Now I add few extra methods and a String member variable. I’ll add a main() method as well.

Output:

I think you can easily understand how the code works by just looking at it. All I wanted to show is enums are not much different from usual Classes and Enum objects are quite same as the normal objects.

There’s an important point you must notice. We don’t use the keyword “new” to make an Enum object.

What we do is this:-

There’s a static method in an Enum called values() which will return an array of all the objects in the Enum. We can use this to print all the objects inside the Enum like this, by using a for each loop.

In the main() method,

Output:

Moreover, since we can traverse through each object by using this method, it is easy to call a method for each item like this;

Again, I use the main() method. But note that, I have added a few extra lines above the for-each loop.

Output:

Are Enum Constants Immutable?

I’ll give the answer at the end. First, the meaning of this question is, can’t we change the values of Enum constants? For example, can we add another Enum constant in addition to the 4 constants that we have created? Can’t we make 2 AUTUMN objects? Can we delete one object and add another one instead? Answer for all is, we can’t. Let’s see.

In the previous code I have used the line

in order to make the output of for each loop distinguishable from the previous output. In the previous output, we have assigned the Enum object AUTUMN in to the variable mySeason and called the setSeasonDetails() method to add a detail to the mySeason variable. But look sharply. The same detail is printed for the AUTUMN in for-each loop as well. Why? We made the change to the mySeason object! So why it is printed? Shouldn’t we call mySeason.getInfo() instead of s.getInfo()? NO!! Enum objects are constants. They are created once and will NOT change. We cannot create new objects of Seasons so the existing 4 objects are the only available ones. See for yourself by modifying the main() method as follows:

Output:

We didn’t call the setSeasonDetails() method for mySeason2 variable. If a new AUTUMN object is created in here, the output for the mySeason2.getInfo() should be

The same reference for the AUTUMN object is assigned to both mySeason and mySeason2. Similarly, same AUTUMN in mySeason and s of the for-each loop.

Once we create two Enum variables, if we assign the same enum constant to both, then, they refer to the same Object.

So the answer to the question whether enum constants are immutable is: YES! Partially it’s true.

What about state? Aren’t we changing it?

True. We can of course change the state of the enum constants.

State means the values that are stored in instance variables of the Enum constants. For example, seasonDetails variable available in all 4 Seasons objects is an instance variable. We have changed the value stored in AUTUMN object in the previous example. So we are ultimately changing the state of the Enum constant AUTUMN.

If enum constant objects are REALLY immutable, then why can we mutate the object?

Yes, you can change the state of the object. But you can’t change the object itself. Though your look changes when you change clothes, your inside don’t change. Right?

Can We Make Enum Constants Fully Immutable?

Sure. Just make all instance variables are immutable. Remove all the setter methods in your program. Declare all the member variables final.

Then we can make constructors to initialize this final String variable.

Now when we declare the enum constants, we can assign a value for the seasonDetails.

This won’t change after that. So, our enum constants are now fully immutable!