Last Updated: 28 May, 2022
Enum (also known as Enumeration) was added in Java 5, Enum is a special type of Data-type like Class and Interface. Enum is used to define collections of constants (unchangeable variables) in Java.
The enum
keyword is used to declare enums in Java, enum can be declared inside a class or not outside a class but not inside in a method. As per the naming convention, we should already write enums in uppercase (capital letters).
By default all Java enum constants are public, static and final.
Syntax:
enum enum_name { CONSTANT1, CONSTANT2, CONSTANT3 }
Use enum keyword to create an enum in Java, An enum can be defined simply by creating a list of enum constants and separating the constants with a comma (,) sign.
Let's understand enum with the help of given example.
Output
TUESDAY
Use Enums in if-else Statements
Output
Medium Size
Use Enums in Switch Statements
Output
Large Size
Important points about 'enum' in Java
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What properties an Enum can contain inside?
Ans. An Enum can contain the following properties: