Java Enum
Java Enum
Why Enum?
What is Enum?
• Enums in java are mainly used for grouping similar kind of constants as a
one unit. constants means static and final.
• Enums are introduced in JDK 1.5 onward.
• Before that similar kind of constants are grouped by declaring them as
static and final in one class.
How to Define and Use an Enumeration
• An enumeration can be defined simply by creating a list of enum
variable. Let us take an example for list of Subject variable, with
different subjects in the list.
• Identifiers Java, spring, angular and HTML are called enumeration
constants. These are public, static and final by default.
• Variables of Enumeration can be defined directly without any
new keyword.
Important points about enum
1. It is a special data that is used to define a set of constants and
the value of constant define at compile time.
2. All the constant of enum are by default static and final. So, we
can’t change the value of the constant. As constants are static
so they are accessible by enum name.
3. A enum can have methods, constructor and fields.
4. Java enums extends the Enum class that exists in java.lang
package. The Enum class is an abstract class that implements a
Comparable interface and serialize interface.
5. A enum can implements interfaces but can’t extend the class
because it already internally extending the Enum class.
Points to remember about Enumerations
• Enumerations are of class type, and have all the capabilities that a Java
class has.
• Enumerations can have Constructors, instance Variables, methods and
can even implement Interfaces.
• Enumerations are not instantiated using new keyword.
• All Enumerations by default inherit java.lang.Enum class.