1 / 9

Object Oriented Programming using Java - Enumerations

Department of Computer and Information Science, School of Science, IUPUI. Object Oriented Programming using Java - Enumerations. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Enumerations . enum types Declared with an enum declaration

val
Download Presentation

Object Oriented Programming using Java - Enumerations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Department of Computer and Information Science,School of Science, IUPUI Object Oriented Programming using Java- Enumerations Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

  2. Enumerations • enum types • Declared with an enum declaration • A comma-separated list of enum constants • Declares an enum class with the following restrictions: • enum types are implicitly final • enum constants are implicitly static • Attempting to create an object of an enum type with new is a compilation error • enum constants can be used anywhere constants can • enum constructor • Like class constructors, can specify parameters and be overloaded

  3. Outline Declare six enum constants • Book.java • (1 of 2) Arguments to pass to the enum constructor Declare instance variables Declare enum constructor Book

  4. Outline • Book.java • (2 of 2)

  5. Enumerations (Cont.) • static method values • Generated by the compiler for every enum • Returns an array of the enum’s constants in the order in which they were declared • static method range of class EnumSet • Takes two parameters, the first and last enum constants in the desired range • Returns an EnumSet containing the constants in that range, inclusive • An enhanced for statement can iterate over an EnumSet as it can over an array

  6. Outline • EnumTest.java • (1 of 2) Enhanced for loop iterates for each enum constant in the array returned by method value Enhanced for loop iterates for each enum constant in the EnumSet returned by method range

  7. Outline • EnumTest.java • (2 of 2)

  8. Common Programming Error 8.6 • In an enum declaration, it is a syntax error to declare enum constants after the enumtype’s constructors, fields and methods in the enum declaration.

  9. Acknowledgements • Deitel, Java How to Program

More Related