Enum Methods
layout: default
title: enum Method
Every enum is a class, and every enum constant is an instance, so each has methods.
enum Constant Methods¶
boolean equals(Object)¶
This is the same as other .equals methods.
- Using
==will yield the same result, because there is only one of each constant in anenum.
toString()¶
Returns the name of this enum constant, as contained in the declaration.
System.out.println(Color.RED); // RED
int ordinal()¶
Each constant has a zero-based number for its position in the enum declaration.
int pos = Color.RED.ordinal(); // 0
int compareTo(otherEnumConstant)¶
Every enum implements Comparable, so it can compare itself to other constant values.
* Natural order is determined by order in the enum definition.
int compResult = Color.RED.compareTo(Color.GREEN);
System.out.println(compResult); // -1, RED comes before GREEN
static Methods¶
The enum itself has static methods.
static E[] values()¶
This returns an array of the constants in this enum, in the order listed in the enum.
Color[] values = Color.values();
E valueOf(String)¶
This will return the enum for a String value that matches one of the constants.
Color val = Color.valueOf("RED"); // val = Color.RED
Color nope = Color.valueOf("PURPLE"); // IllegalArgumentException
Drill¶
EnumeratedTypes/com.example.enums.drills.DayMethods
- Iterate through the
Dayconstants, printing eachDayname.- If the name contains the letter R, add a
*next to the name.