0% found this document useful (0 votes)
29 views

Java Enums

The document discusses Java enums, which define a fixed set of constants like days of the week or directions. Enums can have fields, constructors, methods, and implement interfaces like classes, but internally extend the Enum class and cannot extend other classes. Enums improve type safety, can be used in switch statements, traversed, and have constants in uppercase according to naming conventions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Java Enums

The document discusses Java enums, which define a fixed set of constants like days of the week or directions. Enums can have fields, constructors, methods, and implement interfaces like classes, but internally extend the Enum class and cannot extend other classes. Enums improve type safety, can be used in switch statements, traversed, and have constants in uppercase according to naming conventions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Enums

The Enum in Java is a data type which contains a fixed set of constants.

It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, and SATURDAY) , directions (NORTH, SOUTH, EAST, and WEST),
season (SPRING, SUMMER, WINTER, and AUTUMN or FALL), colors (RED, YELLOW, BLUE,
GREEN, WHITE, and BLACK) etc. According to the Java naming conventions, we should
have all constants in capital letters. So, we have enum constants in capital letters.

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. It is
available since JDK 1.5.

Enums are used to create our own data type like classes. The enum data type (also
known as Enumerated Data Type) is used to define an enum in Java. Unlike C/C++,
enum in Java is more powerful. Here, we can define an enum either inside the class or
outside the class.

Java Enum internally inherits the Enum class, so it cannot inherit any other class, but it
can implement many interfaces. We can have fields, constructors, methods, and main
methods in Java enum.

Points to remember for Java Enum


o Enum improves type safety
o Enum can be easily used in switch
o Enum can be traversed
o Enum can have fields, constructors and methods
o Enum may implement many interfaces but cannot extend any class because it
internally extends Enum class
Simple Example of Java Enum

You might also like