Enum - Singleton Pattern - InstanceOf
Enum - Singleton Pattern - InstanceOf
Enum - Singleton Pattern - InstanceOf
By
Arvind Kumar
Asst. Professor, LPU
Enum Types
• An enum 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.
• enum Day
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY;
}
• The identifiers SUNDAY, MONDAY, and so on, are called
enumeration constants.
• Each is implicitly declared as a public, static final member of
Day.
Enum Types
• A Java enumeration is a class type. Although you don’t
instantiate an enum using new.
• Once you have defined an enumeration, you can create a
variable of that type :
Day d;
• Because d is of type Day, the only values that it can be
assigned (or can contain) are those defined by the
enumeration.
d = Day. MONDAY ;
• Two enumeration constants can be compared for equality by
using the = = relational operator. For example,
• Note: An enumeration value can also be used to control a
switch statement.
Enum Types
• Java Enumerations are Class Types
//illegal construct
//Compile Time Error: The constructor SingleObject() is not visible
//SingleObject object = new SingleObject();