Object-Oriented Programming: Enum types An enum type is a type whose fields consist of a fixed set of constants; Because they are constants, the names of an enum type's fields are in uppercase letters. | Lecture 3-bis: Enum types Lê Hồng Phương phuonglh@ Department of Mathematics, Mechanics and Informatics, Vietnam National University, Hanoi Enum types ● An enum type is a type whose fields consist of a fixed set of constants. – – The days of the week – ● Compass directions The suits of a card Because they are constants, the names of an enum type's fields are in uppercase letters. 2012-2013 Object-Oriented Programming: Enum types 2 Enum types public enum CompassDirection { NORTH, SOUTH, EAST, WEST } public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public enum Suit { SPADE, CLUB, DIAMOND, HEART } 2012-2013 Object-Oriented Programming: Enum types 3 The Card class revisited public class Card { private int rank; private String suit; public Card(int rank, String suit) { = rank; = suit; } // . } public class CardTester { public static void main(String[] args) { Card c1 = new Card(2, "Spade"); Card c2 = new Card(2, "Undefined"); } } 2012-2013 Object-Oriented Programming: Enum types We should prevent user from doing this. 4 The Card class revisited public class Card { private int rank; private Suit suit; public Card(int rank, Suit suit) { = rank; = suit; } // . } public class CardTester { public static void main(String[] args) { Card c1 = new Card(1, ); } } 2012-2013 There are only 4 possible suits that a card can take. Object-Oriented Programming: Enum .