Enum
Overview of Enum in Java.
About
Enums in Java are a special data type that represents a fixed set of constants. This is useful when we need a variable to hold a limited set of values, like days of the week or card suits. Enums in Java are also full-fledged classes with some special characteristics and limitations.
Key Characteristics of Enums
Fixed Set of Constants: Enums represent a fixed set of predefined constants, such as
RED
,GREEN
, andBLUE
in aColor
enum.Type-Safe: Enums provide compile-time type safety, ensuring that a variable only holds valid values from the specified enum type.
Extends
java.lang.Enum
: Every enum implicitly extendsjava.lang.Enum
, making them unique from other classes.Cannot be Instantiated: Enums are implicitly
final
, meaning new instances cannot be created, and they cannot be subclassed.Utility Methods: Enums come with built-in methods like
.values()
,.name()
, and.ordinal()
.
Example of an Enum
Enums can be used directly to restrict a variable’s possible values
Thread Safety of Enums
Enums in Java are indeed thread-safe by design, and they offer several additional advanced features that make them both versatile and robust in multi-threaded environments.
Immutable by Nature: Enums are inherently immutable because each enum constant is a static final instance, meaning it can only be created once and cannot be modified. This immutability makes enums thread-safe by default, as multiple threads accessing an enum instance can only read the constant’s state without risking data inconsistency or concurrency issues.
Static Initialization: Enums are initialized at class loading time, in a thread-safe manner, following the Java Class Loading Mechanism. The JVM guarantees that each enum constant is only instantiated once and that no two threads can initialize the same enum class simultaneously, further reinforcing thread safety.
Enums as Singletons
Singleton Design Pattern: Enums can be used to implement singletons easily and reliably in Java, since the JVM guarantees that each enum constant is instantiated only once. This makes enums a preferred approach for implementing singletons over traditional patterns, which require additional measures to ensure thread safety.
Serialization Safety: Enums provide built-in serialization safety. During deserialization, the JVM ensures that the same instance is returned, avoiding the risk of creating duplicate instances of a singleton.
Example Singleton Implementation Using Enum:
Usage:
In the above DatabaseConnection
enum example, the purpose is to use an enum to implement a singleton pattern for managing a database connection.
INSTANCE;
:
This is a single enum constant, which represents the single instance of
DatabaseConnection
.This
INSTANCE
constant allows us to access the singleDatabaseConnection
object throughout the application by referencingDatabaseConnection.INSTANCE
.
private Connection connection;
:
This field represents a database connection that we would typically use to interact with a database.
Here,
Connection
is usually a class from a database library (likejava.sql.Connection
), which represents an active connection to the database.
DatabaseConnection()
Constructor:
This constructor initializes the singleton instance. In this pattern, the constructor is called only once, at the time of enum class loading.
Inside this constructor, we would normally include code to initialize the
connection
field, such as establishing a connection to a database.
public Connection getConnection()
Method:
This method provides controlled access to the
connection
object.Any part of our application that needs to use this database connection can call Connection conn =
DatabaseConnection.INSTANCE.getConnection()
.By exposing
getConnection()
, the enum ensures that there’s only ever one connection in use, avoiding the creation of multiple connections across the application.
Enums with Fields, Methods, and Constructors
Custom Fields and Methods: Enums in Java are more than simple lists of constants. Each constant can hold specific fields, methods, and even implement interfaces, making enums highly flexible and allowing for behavior-based usage.
Example with Additional Fields and Methods:
Customizing Behavior per Constant: Enums can override methods per constant, allowing for complex behaviors to be tied to individual enum values.
Advantages of Using Enums
Compile-Time Safety: Prevents invalid values from being assigned to variables.
Code Readability: Makes code more readable by using meaningful constant names.
Consistent and Centralized: The allowed values are defined in a single place.
Enum Limitations
Lack of Subclassing: Enums cannot be subclassed as they implicitly extend
java.lang.Enum
.Inability to Directly Extend Enums: While enums cannot extend other classes, they can implement interfaces, allowing for some degree of abstraction and flexibility.
Fixed Constants: Enum constants are static and final by default, so they cannot be changed once defined. This is a benefit for immutability but limits flexibility when constants need to evolve over time.
Last updated
Was this helpful?