HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables)
An enum
is a special "class" that represents a group of
constants (unchangeable variables, like final
variables).
To create an enum
, use the enum
keyword (instead of class or interface), and separate
the constants with a comma. Note that they should be in uppercase letters:
enum Level {
LOW,
MEDIUM,
HIGH
}
You can access enum
constants with the dot syntax:
Level myVar = Level.MEDIUM;
Enum is short for "enumerations", which means "specifically listed".
You can also have an enum
inside a class:
public class Main {
enum Level {
LOW,
MEDIUM,
HIGH
}
public static void main(String[] args) {
Level myVar = Level.MEDIUM;
System.out.println(myVar);
}
}
The output will be:
MEDIUM
Enums are often used in switch
statements to check for corresponding values:
enum Level {
LOW,
MEDIUM,
HIGH
}
public class Main {
public static void main(String[] args) {
Level myVar = Level.MEDIUM;
switch(myVar) {
case LOW:
System.out.println("Low level");
break;
case MEDIUM:
System.out.println("Medium level");
break;
case HIGH:
System.out.println("High level");
break;
}
}
}
The output will be:
Medium level
The enum type has a values()
method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum:
for (Level myVar : Level.values()) {
System.out.println(myVar);
}
The output will be:
LOW
MEDIUM
HIGH
An enum
can, just like a class
, have attributes and methods. The only
difference is that enum constants are public
, static
and final
(unchangeable - cannot be overridden).
An enum
cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).
Use enums when you have values that you know aren't going to change, like month days, days, colors, deck of cards, etc.