Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus.

Similar presentations


Presentation on theme: "Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus."— Presentation transcript:

1 Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus

2 2 Enumerated Types Java allows you to define an enumerated type, which can then be used to declare variables An enumerated type establishes all possible values for a variable of that type The values are identifiers of your own choosing The following declaration creates an enumerated type called Season enum Season {winter, spring, summer, fall}; Any number of values can be listed

3 3 Enumerated Types Once a type is defined, a variable of that type can be declared Season time; and it can be assigned a value time = Season.fall; The values are specified through the name of the type Enumerated types are type-safe – you cannot assign any value other than those listed

4 4 Ordinal Values Internally, each value of an enumerated type is stored as an integer, called its ordinal value The first value in an enumerated type has an ordinal value of zero, the second one, and so on However, you cannot assign a numeric value to an enumerated type, even if it corresponds to a valid ordinal value

5 5 Enumerated Types The declaration of an enumerated type is a special type of class, and each variable of that type is an object The ordinal method returns the ordinal value of the object The name method returns the name of the identifier corresponding to the object's value See IceCream.javaIceCream.java

6 Fall 2007CS 2256 Enumerated Types In Chapter 3 we introduced enumerated types, which define a new data type and list all possible values of that type enum Season {winter, spring, summer, fall} Once established, the new type can be used to declare variables Season time; The only values this variable can be assigned are the ones established in the enum definition

7 Fall 2007CS 2257 Enumerated Types An enumerated type definition is a special kind of class The values of the enumerated type are objects of that type For example, fall is an object of type Season That's why the following assignment is valid time = Season.fall;

8 Fall 2007CS 2258 Enumerated Types An enumerated type definition can be more interesting than a simple list of values Because they are like classes, we can add additional instance data and methods We can define an enum constructor as well Each value listed for the enumerated type calls the constructor See Season.javaSeason.java See SeasonTester.javaSeasonTester.java

9 Fall 2007CS 2259 Enumerated Types Every enumerated type contains a static method called values that returns a list of all possible values for that type The list returned from values is an iterator, so a for loop can be used to process them easily An enumerated type cannot be instantiated outside of its own definition A carefully designed enumerated type provides a versatile and type-safe mechanism for managing data


Download ppt "Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus."

Similar presentations


Ads by Google