Java: Enumerations
Enumerations are kind of like classes, but they are used to declare constants.
To create our enumeration 'class' we do something like this:
enum EnumOne{
Then from here we can create new enum objects, to show you this here is an application which creates and displays peoples names, their pros, and their cons:
enum EnumOne{
Bob("Cool", "Addicted to Cheese"),
Jill("Nice", "Weird"),
Fred("None", "Eats people");
private final String pros;
private final String cons;
EnumOne(String good, String bad){
pros = good;
cons = bad;
}
String GetPros(){
return pros;
}
String GetCons(){
return cons;
}
}
public class Main {
public static void main(String[] args) {
for(EnumOne Object: EnumOne.values()){
System.out.printf("%s:\n%s\n%s\n\n", Object, Object.GetPros(), Object.GetCons());
}
}
}
All this does is create an enumeration with three objects(Bob, Jill and Fred).
Then it names the details we put in about them pros and cons; Next it creates a constructor and two functions, what they do are pretty obvious.
Then using an enhanced for statement in the main method we go through all of the different enumerations, the bit you might not understand is:
EnumOne.values()
This simply acts like an array and goes through all of the different enumerations (they are automatically numbered).
Back to Java

