Read over the page on constants first.
Enums address two main problems that using constants have:
making sure that only allowable values can be used.
.e.g. You can set coin=NICKEL, and coin=QUARTER, but never coin=SHILLING or coin=42
making sure that no value of the constant is duplicated.
e.g. you don't accidentally set DIME=10;, and QUARTER=10;
An enum is more-or-less like a class, but a special type of class.
private enum DirType {LEFT, RIGHT, UP, DOWN}; DirType direction = DirType.LEFT; if (direction == DirType.UP) { ... direction = DirType.RIGHT; switch (direction) { case LEFT: ... break; case RIGHT: ... } |
1. Make enums private (normally) ?? Since they are classes, make the class name capitalized The list of constants goes in {} 2. you create a variable (direction) to be this type of object but you never use the word "new" 3. Since constants defined inside Enum in Java are final, you can safely compare them using "==" equality operator. 4. You have to use the enum classname when assigning variables. You can't just say direction = RIGHT; 5. Except in a SWITCH statement! Cool. |
Additional notes:
Enums can have constructors and methods and instance variables too.
See this good website.
Compare the code in these program fragments
1. Using constants instead of enums | 2. Using Enums instead of the constants | 3. Using Enums for constants AND colours |
---|---|---|
import java.awt.Graphics; import java.awt.Color; public class EnumSimple { //Terrain constants: any old number as long as each is unique final static int LAND = 1; //contant for land tile final static int EMPTY = 0; //constant for empty tile final static int LAKE = 33; //this is just any number final static int OCEAN = 89; // used for LAKE, OCEAN final static int SAND = 40; //Colours for terrain final static Color COLOURBACK = new Color(242,242,242); final static Color COLOUREMPTY = new Color(222,222,222); final static Color COLOURLAND = new Color(100,200,100); final static Color COLOURLAKE = new Color(100,100,255); final static Color COLOUROCEAN = new Color(10,10,130); final static Color COLOURSAND= new Color(205,192,176); final static int SIZE = 30; //instance variables int[][]board = new int[SIZE][SIZE]; public static void main(String[] args) { new EnumSimple(); } EnumSimple() { clearBoard(); //makeLand(); //setUpGUI(); } void clearBoard() { for (int i=0;i<SIZE;i++) { for (int j=0;j<SIZE;j++) { board[i][j]=EMPTY; } } } void colourRect(int i, int j, Graphics g) { int terrain = board[i][j]; int blockX = 22; int blockY = 22; if (terrain == EMPTY) { g.setColor(COLOUREMPTY); g.fillRect(blockX*i+1, blockY*j+1, blockX-2, blockY-2); } if (terrain == LAND) { g.setColor(COLOURLAND); g.fillRect(blockX*i+1, blockY*j+1, blockX-2, blockY-2); } if (terrain == LAKE) { g.setColor(COLOURLAKE); g.fillRect(blockX*i+1, blockY*j+1, blockX-2, blockY-2); } } } |
import java.awt.Graphics; import java.awt.Color; public class EnumSimple1 { /* Here the final static constants have been replaced with an ENUM This prevents problems with invalid numbers in board[][], as well as two constants having the same INT value */ //final static int LAND = 1; //final static int EMPTY = 0; //final static int LAKE = 33; //final static int OCEAN = 89; //final static int SAND = 40; private enum Terrain {EMPTY, LAND, LAKE, OCEAN, SAND}; //Colours for terrain final static Color COLOURBACK = new Color(242,242,242); final static Color COLOUREMPTY = new Color(222,222,222); final static Color COLOURLAND = new Color(100,200,100); final static Color COLOURLAKE = new Color(100,100,255); final static Color COLOUROCEAN = new Color(10,10,130); final static Color COLOURSAND= new Color(205,192,176); final static int SIZE = 30; //instance variables //#### int[][] board = new int[SIZE][SIZE]; Terrain[][] board = new Terrain[SIZE][SIZE]; public static void main(String[] args) { new EnumSimple1(); } EnumSimple1() { clearBoard(); //makeLand(); //setUpGUI(); } void clearBoard() { for (int i=0;i<SIZE;i++) { for (int j=0;j<SIZE;j++) { board[i][j]=Terrain.EMPTY; } } } void colourRect(int i, int j, Graphics g) { int blockX = 22; int blockY = 22; if (board[i][j] == Terrain.EMPTY) { g.setColor(COLOUREMPTY); g.fillRect(blockX*i+1, blockY*j+1, blockX-2, blockY-2); } if (board[i][j] == Terrain.LAND) { g.setColor(COLOURLAND); g.fillRect(blockX*i+1, blockY*j+1, blockX-2, blockY-2); } if (board[i][j] == Terrain.LAKE) { g.setColor(COLOURLAKE); g.fillRect(blockX*i+1, blockY*j+1, blockX-2, blockY-2); } } } |
import java.awt.Graphics; import java.awt.Color; public class EnumSimple2 { /**** Here the ENUM also handles the colours ****/ //each enum also contains the colour that it will be painted enum Terrain { EMPTY (new Color(222,222,222)), LAND (new Color(100,200,100)), LAKE (new Color(100,100,255)), OCEAN (new Color(10,10,130)), SAND (new Color(205,192,176)); //instance variables private Color c; //constructor Terrain(Color c) { this.c = c; } //this will give the colour of each terrain Color getColour() { return c; } }; //Colours for terrain final static Color COLOURBACK = new Color(242,242,242); final static int SIZE = 30; //instance variables //### int[][] board = new int[SIZE][SIZE]; Terrain[][] board = new Terrain[SIZE][SIZE]; public static void main(String[] args) { new EnumSimple2(); } EnumSimple2() { clearBoard(); //makeLand(); //setUpGUI(); } void clearBoard() { for (int i=0;i<SIZE;i++) { for (int j=0;j<SIZE;j++) { board[i][j]=Terrain.EMPTY; } } } void colourRect(int i, int j, Graphics g) { int blockX = 22; int blockY = 22; //note that this handles ALL the terrain painting! Terrain t = board[i][j]; g.setColor(t.getColour()); g.fillRect(blockX*i+1, blockY*j+1, blockX-2, blockY-2); } } |
How it works: see the third column above
enum Terrain { EMPTY (new Color(222,222,222)), LAND (new Color(100,200,100)), LAKE (new Color(100,100,255)), OCEAN (new Color(10,10,130)), SAND (new Color(205,192,176)); //instance variables private Color c; //constructor Terrain(Color c) { this.c = c; } //this will give the colour of each terrain Color getColour() { return c; } }; |
1. Create the enum 2. each of these lines is an ENUM constant Following the name of the constant is () - which is a list of parameters to send to the constructor. 3. You'll always need an instance variable to store what is passed to the constructor. This MUST be private! 4. The constructor takes the parameter passed and copies it to the instance variable. 5. Write a method to return the instance variable |
board[3,7] = Terrain.LAND; Terrain t = board[i][j]; g.setColor(t.getColour()); |
How to use in code |
Look at the following examples for how to use Enums efficiently and how to use the ordinal() and name() methods.
public class MyEnum { enum Days{ MONDAY, WEDNESDAY, SATURDAY; private String text=""; Days(){ switch (this.ordinal()) { case 0: text="It sucks"; break; case 1: text="Half way there"; break; case 2: text="Weekend!"; break; default: text="huh?"; } } String getText() { return text; } } public static void main(String[ ]args) { Days day = Days.MONDAY; System.out.println(day.getText()); day = Days.WEDNESDAY; System.out.println(day.getText()); } } |
Enum definition The enums that are created Constructor for enum class ordinal() gives the Array[] index of the enum element MONDAY is 0, WEDNESDAY is 1, etc |
public class EnumExample { enum Days{ MONDAY("It sucks"), WEDNESDAY("Half way through"), SATURDAY("Weekend"); private String text = "huh?"; Days(String s) { this.text = s; } String getText() { return name() + ": " + text; } } public static void main (String args[]) { Days day = Days.MONDAY; System.out.println(day.getText()); day = Days.WEDNESDAY; System.out.println(day.getText()); } } |
Enum definition can have information that is passed to the constructor Constructor for enum class assigns each string to the correct enum element name() returns the name of the current enum |
This prints:MONDAY: It sucks WEDNESDAY: Half way through |