Java Enums
An evaluation of Java Enums is presented in this article, along with examples for better understanding.
Enums
Enums are special “classes” that represent constants (unchangeable variables, like final variables) in Java.
Enums are created by using the enum keyword (instead of classes or interfaces), and constants are separated by commas.
The following should be written in uppercase:
enum Count {
FIRST,
SECOND,
THIRD
}
The dot syntax can be used to access enum constants:
public static void main(String[] args) {Count mrx=Count.THIRD;
System.out.println(mrx);}
}
public class Main {enum Numbers {
ONE,
TWO,
THREE
}public static void main(String[] args) {Numbers ample=Numbers.TWO;
System.out.println(ample);
}
}
Enum means “specifically listed” and is a short form of “enumeration”.
Enum inside a Class
When it comes to Java Enums, you can also have an enum inside a class:
public class Main {
enum Calculator {
ADD,
SUBTRACT,
MULTIPLY,
DIVIDE
}public static void main(String[] args) {Calculator mrx=Calculator.MULTIPLY;
System.out.println(mrx);
}
}
As a result, we will get: MULTIPLY
public class Main {enum Sports {
CRICKET,
FOOTBALL,
TENNIS,
HOCKEY,
RUGBY
}public static void main(String[] args) {
Sports ample=Sports.FOOTBALL;
System.out.println(ample);
}
}
Enum in a Switch Statement
A switch statement often checks for corresponding values using enums:
public class Main {enum NFL_teams{ARIZONA_CARDINALS,
ATLANTA_FALCONS,
CAROLINA_PANTHERS,
BUFFALO_BILLS
}public static void main(String[] args) {
NFL_teams mrx=NFL_teams.ATLANTA_FALCONS;switch (mrx){case ARIZONA_CARDINALS:
System.out.println("The President of Arizona Cardinals is Michael Bidwill");
break;case ATLANTA_FALCONS:
System.out.println("The President of Atlanta Falcons is Arthur Blank ");
break;case CAROLINA_PANTHERS:
System.out.println("The President of Carolina Panthers is David Tepper");
break;case BUFFALO_BILLS:
System.out.println("The President of Buffalo Bills is Terry Pegula");
break;}
}}
Output will be as follows:
public class Main {enum NASA_projects{ARTEMIS_1,
MARS_EXPLORATION_PROGRAM,
APOLLO_PROGRAM,
PROJECT_GEMINI}public static void main(String[] args) {
NASA_projects ample=NASA_projects.MARS_EXPLORATION_PROGRAM;switch (ample){case ARTEMIS_1:
System.out.println("The Artemis 1 mission was started in 2022");
break;case MARS_EXPLORATION_PROGRAM:
System.out.println("The Mars Exploration Program was started in 1994");
break;case APOLLO_PROGRAM:
System.out.println("The Apollo Program was started in 1961");
break;case PROJECT_GEMINI:
System.out.println("The Project Gemini was started in 1965");
break;}
}}
Here is the output:
Loop Through an Enum
An array of enum constants can be retrieved by the values() method of the enum type. Using this method, you can loop through an enum’s constants:
public class Main {enum MLB_teams {CHICAGO_WHITE_SOX,
CHICAGO_CUBS,MINNESOTA_TWINS,PITTSBURGH_PIRATES,CLEVELAND_GUARDIANS}public static void main(String[] args) {for (MLB_teams mrx : MLB_teams.values()) {System.out.println(mrx);
}
}}
As a result, we will get:
CHICAGO_WHITE_SOX
CHICAGO_CUBS
MINNESOTA_TWINS
PITTSBURGH_PIRATES
CLEVELAND_GUARDIANS
public class Main {enum Famous_Universities {HARVARD_UNIVERSITY,
MASSACHUSETTS_INSTITUTE_OF_TECHNOLOGY,
STANFORD_UNIVERSITY,
UNIVERSITY_OF_CAMBRIDGE,
UNIVERSITY_OF_OXFORD}public static void main(String[] args) {for (Famous_Universities ample: Famous_Universities.values()) {System.out.println(ample);
}}}
Therefore, we will obtain:
HARVARD_UNIVERSITY
MASSACHUSETTS_INSTITUTE_OF_TECHNOLOGY
STANFORD_UNIVERSITY
UNIVERSITY_OF_CAMBRIDGE
UNIVERSITY_OF_OXFORD
Enums and classes: Differences and similarities
Like a class, an enum can have attributes and methods.
Unlike class constants, enum constants are public, static, and final (unchangeable).
It is not possible to create objects or extend other classes with an enum (but it is possible to implement interfaces with an enum).
Enums: Why And When Do You Use Them?
You should use Java enums when you have values that won’t change over time, like months, days, colors, decks of cards, etc.