ENUM NOTES:-
The Enum in Java is a data type which contains a fixed set of constants.
Java Enums can be thought of as classes which have a fixed set of constants
(a variable that does not change). The Java enum constants are static and
final implicitly. We can define an enum either inside the class or outside
the class.
o Enum ensure strong compile type safety
o Enum can be easily used in switch
o Enum can be traversed {through values()}
o Enum can have fields, constructors and methods as it is also class type
o Enum may implement many interfaces but cannot extend any class because it
internally exten$
o Provides separate name space for each enum type as such same enumerated
constant may appea$
o Constants are not compled into client codes hence can be freely add, remove or
reorder the$
Eg. enum Status { PASS, FAIL;}
enum Direction {NORTH , EAST , SOUTH, WEST;}
Example 1.1:-
enum Color {
RED,
GREEN,
BLUE;
// Driver method
public static void main(String[] args)
{
Color c1 = Color.RED;
System.out.println(c1);
}
}
Example 1:-
class EnumExample1{
//defining the enum inside the class
public enum Season { WINTER, SPRING, SUMMER, FALL }
//main method
public static void main(String[] args) {
//traversing the enum
for (Season s : Season.values())
System.out.println(s);
}}
Example 2:-
// Enum Declared
enum Color {
RED,
GREEN,
BLUE;
The Enum in Java is a data type which contains a fixed set of constants.
Java Enums can be thought of as classes which have a fixed set of constants
(a variable that does not change). The Java enum constants are static and
final implicitly. We can define an enum either inside the class or outside
the class.
o Enum ensure strong compile type safety
o Enum can be easily used in switch
o Enum can be traversed {through values()}
o Enum can have fields, constructors and methods as it is also class type
o Enum may implement many interfaces but cannot extend any class because it
internally exten$
o Provides separate name space for each enum type as such same enumerated
constant may appea$
o Constants are not compled into client codes hence can be freely add, remove or
reorder the$
Eg. enum Status { PASS, FAIL;}
enum Direction {NORTH , EAST , SOUTH, WEST;}
Example 1.1:-
enum Color {
RED,
GREEN,
BLUE;
// Driver method
public static void main(String[] args)
{
Color c1 = Color.RED;
System.out.println(c1);
}
}
Example 1:-
class EnumExample1{
//defining the enum inside the class
public enum Season { WINTER, SPRING, SUMMER, FALL }
//main method
public static void main(String[] args) {
//traversing the enum
for (Season s : Season.values())
System.out.println(s);
}}
Example 2:-
// Enum Declared
enum Color {
RED,
GREEN,
BLUE;