Save
Freshman
CP2
Pre-Finals
Save
Share
Learn
Content
Leaderboard
Learn
Created by
Joshua
Visit profile
Cards (20)
Enumeration
(enum)
A data type that contains a fixed set of
constants
View source
Enums are
good
to use when you already know all possibilities of the
values
or instances of the class
View source
With
enums
, it is impossible to create an invalid enum value without introducing a compiler error
View source
Examples of when enumerations are used
Compass directions
Months
of the year
Cards
in a deck
View source
Creating an enum
1. Declare a
type
with the enum keyword
2. Identifier for the
type
3. List of values called enum
constants
View source
Enum constants
The only allowed values for the
type
View source
Enum
type
A class, with enum
constants
acting like objects instantiated from the
class
View source
Built-in enum methods
toString
()
ordinal
()
equals
()
compareTo
()
View source
valueOf()
Accepts a string
parameter
and returns an enumeration
constant
View source
values()
Returns an
array
of the enumerated
constants
View source
Declaring an
enum
In its own
file
or within a
class
but not within a method
View source
Enum usage
Selecting a
grading period
Controlling a
switch
structure
View source
Type-safe
Allows only
appropriate
behaviors
View source
Nested class
A
class created within another class
View source
Types of nested classes
Static member class
Non-static member class (inner class)
Local
class
Anonymous
class
View source
Reason for nesting a class
The
inner
class is used only by the
top-level
class
View source
Nested class example
RealEstateListing
class with
HouseData
inner class
View source
An inner class can access its
top-level
class's fields and methods, even if they are
private
View source
An
outer
class can access its
inner
class's members
View source
You usually will not want to create
inner classes
, as they are only usable in the class they
reside
in
View source