Java Enum
Introduction
Enum in java is a data type that contains fixed set of
constants.
It can be thought of as a class having fixed set of
constants.
The java enum constants are static and final implicitly.
It is available from JDK 1.5.
It can be used to declare days of the week, Months in a
Year etc.
Advantages of Enum
enum improves type safety
enum can be easily used in switch
enum can be traversed
enum can have fields, constructors and methods
enum may implement many interfaces but cannot
extend any class because it internally extends Enum
class
values() method
The java compiler internally adds the values() method
when it creates an enum.
The values() method returns an array containing all the
values of the enum.
ordinal() and valueOf()
• Order is important in enums. By using ordinal()
method, each enum constant index can be found.
• valueOf() method returns the enum constant of
the specifed string value, if exists.
Important
Enum can not be instantiated using new keyword because it contains private
constructors only.
We can have abstract methods and can provide the implementation of these
methods in an Enum.
The enum can be defined within or outside the class because it is similar to a
class.
Every enum constant is always implicitly public static final.
We can declare main() method inside enum.
Every enum constant represents an object of type enum.
Example
class EnumDemo
{
public enum Season { SUMMER, WINTER, SPRING}
public static void main(String[] rk)
{
for (Season s : Season.values())
System.out.println(s);
}
}
Example 2
public enum Season { SUMMER, WINTER, SPRING}
class EnumDemo2
{
public static void main(String[] rk)
{
for (Season s : Season.values())
System.out.println(s);
}
}
Enum with Constructor
public enum MonthWithDays {
January(31), February(28), March(31);
int days;
MonthWithDays(int a) { days = a; }
}
class EnumDemo3
{
public static void main(String[] rk)
{
for (MonthWithDays s : MonthWithDays.values())
System.out.println(s);
}
}
Brainstorming 1
Can You declare an enum private?
Can you define an enum inside any block in a class?
Can you define multiple enums inside same class?
Let’s Do Something
Write a program which accepts an array of marks (N subjects)
containing double type values and return the array of marks
after rounding up the marks of all the subjects.
• What is the order of variables in Enum?
a) Ascending order
b) Descending order
c) Random order
d) Depends on the order() method
• Can we create an instance of Enum outside of
Enum itself?
a) True
b) False
What will be the output of the following Java
code?
enum Season
{
WINTER, SPRING, SUMMER, FALL
};
System.out.println(Season.WINTER.ordinal());
What will be the output of the following Java
code snippet?
class A
{}
enum Enums extends A
{
ABC, BCD, CDE, DEF;
}
What will be the output of the following Java
code snippet?
enum Levels
{
private TOP,
public MEDIUM,
protected BOTTOM;
}
What will be the output of the following Java code
snippet?
enum Enums
{
A, B, C;
private Enums()
{System.out.println(10);}}
public class MainClass
{ public static void main(String[] args)
{Enum en = Enums.B;
}}
Which method returns the elements of Enum
class?
a) getEnums()
b) getEnumConstants()
c) getEnumList()
d) getEnum()
Which class does all the Enums extend?
a) Object
b) Enums
c) Enum
d) EnumClass
Are enums are type-safe?
a) True
b) False