You can do it as follows:
private enum TrafficLight {
// using the constructor defined below
RED("abc"),
GREEN("def");
// Member to hold the name
private String string;
// constructor to set the string
TrafficLight(String name){string = name;}
// the toString just returns the given name
@Override
public String toString() {
return string;
}
}
You can add as many methods and members as you like. I believe you can even add multiple constructors. All constructors must be private
.
An enum
in Java is basically a class
that has a set number of instances.