Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // quick introduction to java enums
- // for eggmun @ rizon
- // by ed @ rizon
- public class Eggmun01 {
- public static void main(String[] args) {
- // trick to avoid having to static everything
- new Eggmun01().rin(args);
- }
- enum Color
- {
- RED, GREEN, BLUE, YELLOW
- }
- // our non-static main()
- public void rin(String[] args)
- {
- Color one = Color.BLUE;
- Color two = Color.YELLOW;
- // first example; most frequent usage
- if (one == Color.BLUE)
- {
- System.out.println("Color one is blue");
- }
- else
- {
- System.out.println("Color one is not blue");
- }
- // second example; using an enum value as:
- // - a string (implicit typecasting)
- // - or int (explicit typecasting)
- System.out.println("Color one is " + one + " which is color " + one.ordinal() + " in the enum");
- // third example; comparing enum variables
- if (one == two)
- {
- System.out.println("The colors match");
- }
- else
- {
- System.out.println("The colors does not match");
- }
- // fourth example; list colors in the enum
- Color[] yeah = Color.values();
- for (int i = 0; i < yeah.length; i++)
- {
- System.out.print(yeah[i] + ", ");
- }
- System.out.println();
- // fifth example; echo color #n
- System.out.print("The 2nd color is " + Color.values()[2]);
- // HTH
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment