C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: In the if-statement, we test the values of Importance. For Low, we display 1. For Medium and High, we display 2 or 3.
IfResult: It matches Importance.Medium, so the value 2 is written to the console. We use System.out.println for this function.
ConsoleJava program that uses enum
public class Program {
enum Importance {
Low,
Medium,
High
}
public static void main(String[] args) {
// Create an Importance variable.
Importance i = Importance.Medium;
// Test the Importance.
if (i == Importance.Low) {
System.out.println("1");
} else if (i == Importance.Medium) {
System.out.println("2");
} else if (i == Importance.High) {
System.out.println("3");
}
}
}
Output
2
Qualified: Sometimes we use qualified constants, which include the enum type. An example of this syntax is "Size.Small."
Unqualified: In a switch, though, we must use unqualified constants. An example of this form is "Small."
SwitchJava program that uses method, switch
public class Program {
enum Size {
Small, Medium, Large
}
static void test(Size size) {
// Switch on the enum value.
switch (size) {
case Small:
System.out.println("?");
return;
case Medium:
System.out.println("Is medium size");
return;
case Large:
System.out.println("!");
return;
}
}
public static void main(String[] args) {
// Call method and pass enum value to it.
test(Size.Medium);
}
}
Output
Is medium size
Tip: With unqualified enum constants in a switch, we reduce code size and make code easier to read.
Qualified case label error
The qualified case label Program.Size.Small
must be replaced with the unqualified enum constant Small
Tip: We demonstrate a null-initialized static field here. A static field is just like an instance one, except it is tied to no instance.
StaticJava program that shows null enum
public class Program {
enum Color {
Default, Red, Green, Blue
}
static Color c;
public static void main(String[] args) {
// An enum field is null by default.
if (c == null) {
System.out.println("Is null!");
}
// We can assign a null enum variable to a value.
c = Color.Red;
System.out.println(c);
}
}
Output
Is null!
Red
Java program that uses enum array
public class Program {
enum Language {
Java, Python, Ruby
}
public static void main(String[] args) {
// ... Create a three-element array.
Language[] lang = new Language[3];
lang[0] = Language.Java;
lang[1] = null;
lang[2] = Language.Python;
System.out.println(lang[0]);
}
}
Output
Java
Note: An enum can be used as a key type in a HashMap, but the customized EnumMap is faster for this situation.
Java program that uses ArrayList, enums
import java.util.ArrayList;
public class Program {
enum Density {
Low, Medium, High
}
public static void main(String[] args) {
// Add enums to an ArrayList.
ArrayList<Density> list = new ArrayList<>();
list.add(Density.Medium);
list.add(Density.High);
// ... Display enums.
for (Density value : list) {
System.out.println(value);
}
}
}
Output
Medium
High
Ordinal: This returns the enum value's index within the enum. The first entry has an ordinal of 0 and the second has an ordinal of 1.
Java program that uses name, ordinal
enum DataCode {
Image, Text, Combination
}
public class Program {
static void test(DataCode code) {
// Write name and ordinal of these enums.
System.out.println(code.name());
System.out.println(code.ordinal());
}
public static void main(String[] args) {
// Call the test method.
test(DataCode.Image);
test(DataCode.Text);
test(DataCode.Combination);
}
}
Output
Image
0
Text
1
Combination
2