KaiquanMah's picture
enum Topic1 {VALUE1, ...}; "someStr" == Topic1.VALUE1;
4142885 verified
Last week we saw a few examples of storing constant values as static variables in a class.
In older versions of Java, it was common to use static variables in this way:
class Suit {
public static final int SPADES = 1;
public static final int HEARTS = 2;
public static final int CLUBS = 3;
public static final int DIAMONDS = 4;
}
Now, the playing card class could look something like this:
class PlayingCard {
private int suit;
private int number;
public PlayingCard(int suit, int number) {
this.suit = suit;
this.number = number;
}
public int getSuit() {
return suit;
}
public int getNumber() {
return number;
}
}
...and a new playing card could be created like this:
public static void main(String[] args) {
PlayingCard sevenOfSpades = new PlayingCard(Suit.SPADES, 7);
}
===============================================================
However, the method has a clear shortcoming:
the constant numerical values do not automatically associate with their meanings.
Even if we agreed that the numerical value 1 corresponds to the suit of spades in a deck of cards,
requesting the suit will nevertheless return an integer:
public static void main(String[] args) {
PlayingCard sevenOfSpades = new PlayingCard(Suit.SPADES, 7);
System.out.println("Card's suit: " + sevenOfSpades.getSuit());
System.out.println("Card's number: " + sevenOfSpades.getNumber());
}
The program prints:
Card's suit: 1
Card's number: 7
A better solution is to use the enum class introduced in Java 5.
==============================================
What is an enum class?
The purpose of an enum class is to define a SET of VALUES.
Typical examples could be
the suits in a card game,
compass directions or
the houses at Hogwarts.
The class is defined with the keyword 'enum'.
In its simplest form, we just write the values that belong to the set
inside the class definition, separated by commas:
enum Suit {
SPADES, HEARTS, CLUBS, DIAMONDS
}
Now we can change the definition of the card game class so that the suit is of type 'Suit', not an integer.
This also limits the possible values: the suit of the card cannot be anything other than one of the four defined in the enum class Suit.
class PlayingCard {
private Suit suit;
private int number;
public PlayingCard(Suit suit, int number) {
this.suit = suit;
this.number = number;
}
public Suit getSuit() {
return suit;
}
public int getNumber() {
return number;
}
}
This is now seen not only when creating a card, but also when printing the card's suit:
public static void main(String[] args) {
PlayingCard sevenOfClubs = new PlayingCard(Suit.CLUBS, 7);
System.out.println("Card's suit: " + sevenOfClubs.getSuit());
System.out.println("Card's number: " + sevenOfClubs.getNumber());
}
Now the program prints:
Card's suit: CLUBS
Card's number: 7
==============================================
Using Enum Values
Enum type values can be directly compared with Java's equality and inequality operators.
// ==
// !=
If we want to check whether a card is a diamond, we can write:
public static void main(String[] args) {
PlayingCard card = new PlayingCard(Suit.DIAMONDS, 9);
if (card.getSuit() == Suit.DIAMONDS) {
System.out.println("The card is a diamond!");
}
}
==============================================
As another example, let's define another enum class, which includes subjects taught:
enum Subject {
MATHEMATICS, PHYSICS, CHEMISTRY, BIOLOGY, SWEDISH
}
Now we can use the value as the type of the 'mainSubject' field in the 'Student' class, for example:
class Student extends Person {
private int credits;
private Subject mainSubject;
public Student(String name, String email, int credits, Subject mainSubject) {
super(name, email);
this.credits = credits;
this.mainSubject = mainSubject;
}
public int getCredits() {
return credits;
}
public Subject getMainSubject() {
return mainSubject;
}
}
Now it's easy to write a method, for example, that picks out the mathematics students from the student list:
public static ArrayList<Student> mathematicians(ArrayList<Student> students) {
ArrayList<Student> al = new ArrayList<>();
for (Student student : students) {
if (student.getMainSubject() == Subject.MATHEMATICS) {
al.add(student);
}
}
return al;
}
Enum classes also support more versatile (and complex) features, which are not covered in this course.
You can find more information at
https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html