Demystifying Java Enums: Let's Finally Understand Them
Demystifying Java Enums: Let's Finally Understand Them
• ABCs of Enum
• Enums under the hood
• When to use Enums?
• Use enums instead of int constant
• Use instance elds instead of ordinals
• Use EnumMap instead of ordinal indexing
• Implement Design Patterns Using Enums
• Singleton Pattern
• Strategy Pattern
© Akshita Chawla
fi
A..B..C..s of Enum
An Enumerations—or “enums” for short type , is a special data type that enables for a variable to be a set of predefined constants.
The variable must be equal to one of the values that have been predefined for it.
© Akshita Chawla
fi
fi
fi
Enums :
What you see vs What JVM sees
© Akshita Chawla
Enum : Under the Hood
© Akshita Chawla
fi
© Akshita Chawla
When to use enums ? Use Enums instead of int constants
Before enum types were added to the language, a common pattern for
representing enumerated types was to declare a group of named int constants,
one for each member of the type
Programs that use int enums are brittle. Because int enums are
constant variables, their int values are compiled into the clients that
use them.If the value associated with an int enum is changed, its
clients must be recompiled. If not, the clients will still run, but their
behavior will be incorrect.
© Akshita Chawla
fi
When to use enums ? Use Enums instead of int constants
Type Instance
Safe Controlled
© Akshita Chawla
ff
fi
When to use enums ? Use Enums instead of int constants
Sometimes you need to associate fundamentally different behavior with each constant
Better way to do it
© Akshita Chawla
When to use enums ? Use instance fields instead of ordinals
Many enums are naturally associated with a single int value. All enums have an ordinal method, which
returns the numerical position of each enum constant in its type.
Better way to do it
Note The Enum speci cation has this to say about ordinal: “Most
programmers will have no use for this method. It is designed for
use by general-purpose enum- based data structures such as
EnumSet and EnumMap.” © Akshita Chawla
fi
fi
When to use enums ? Use EnumMap instead of ordinal indexing
Class De nition
Better way to do it
EnumMap Approach
This program is shorter, clearer, safer, and comparable in speed to the original version. There is
no unsafe cast; no need to label the output manually because the map keys are enums that
know how to translate themselves to printable strings; and no possibility for error in computing
Using Ordinal Indexing Approach array indices. The reason that EnumMap is comparable in speed to an ordinal-indexed array is
that EnumMap uses such an array internally, but it hides this implementation detail from the
programmer, combining the richness and type safety of a Map with the speed of an array.
© Akshita Chawla
fi
Implement Design Patterns Using Enums
© Akshita Chawla
Singleton Pattern
Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance
Not safe
with deserialization. Not safe
with deserialization.
Singleton with lazy initialization
Let’s think again how did we achieve the singleton behavior in above methods. It was done
by making the constructor private and making inaccessible the constructor to create new
instances of the class.
But the problem is, actually isn’t there any other ways to create an instance of a class other
than the constructor? Answer is no.
Since enums are inherently serializable we don’t need to implement it with serializable interface. Re ection
problem is also not there. Therefore, it is 100% guaranteed that only one instance of the singleton is
present within a JVM.
Conventional Singletons is that they are no longer Singleton once you implement a serializable
interface because the method readObject() always returns a new instance just like the Java
constructor. By using the readResolve() method and discarding newly created instances, you can
avoid that by substituting Singleton
THREAD By default, the Enum instance is thread-safe, and you don’t need
SAFE to worry about double-checked locking.
Safe with
deserialization.
One thing to remember here is when serializing an enum, eld variables are not
get serialized. For example, if we serialize and deserialize above SingletonEnum
class, we will loss the value of the int value eld
Note
© Akshita Chawla
fi
fi
fl
Strategy Pattern
The Strategy Pattern de
This pattern allow us to apply an important concept of
object oriented programming: favor composition over
inheritance, which we can translate as the ability to
change an object’s behavior on runtime.
Traditional
Implementation Enum Implementation
© Akshita Chawla
fi
© Akshita Chawla