Pre-Finals

Cards (20)

  • Enumeration (enum)

    A data type that contains a fixed set of constants
  • Enums are good to use when you already know all possibilities of the values or instances of the class
  • With enums, it is impossible to create an invalid enum value without introducing a compiler error
  • Examples of when enumerations are used
    • Compass directions
    • Months of the year
    • Cards in a deck
  • Creating an enum
    1. Declare a type with the enum keyword
    2. Identifier for the type
    3. List of values called enum constants
  • Enum constants
    • The only allowed values for the type
  • Enum type
    A class, with enum constants acting like objects instantiated from the class
  • Built-in enum methods
    • toString()
    • ordinal()
    • equals()
    • compareTo()
  • valueOf()
    Accepts a string parameter and returns an enumeration constant
  • values()
    Returns an array of the enumerated constants
  • Declaring an enum

    In its own file or within a class but not within a method
  • Enum usage
    • Selecting a grading period
    • Controlling a switch structure
  • Type-safe
    Allows only appropriate behaviors
  • Nested class
    A class created within another class
  • Types of nested classes
    • Static member class
    • Non-static member class (inner class)
    • Local class
    • Anonymous class
  • Reason for nesting a class
    • The inner class is used only by the top-level class
  • Nested class example
    • RealEstateListing class with HouseData inner class
  • An inner class can access its top-level class's fields and methods, even if they are private
  • An outer class can access its inner class's members
  • You usually will not want to create inner classes, as they are only usable in the class they reside in