/**
* Copyright (C) 2022 by Martin Robillard. See https://codesample.info/about.html
*/
package e2.chapter2;
/**
* Represents the suit of a playing card.
*/
public {
CLUBS, DIAMONDS, SPADES, HEARTS;
}
It is generally a poor design choice to rely on primitive types (e.g., int
,
boolean
, and including String
) to represent unrelated concepts.
For example, while it might be tempting to represent a Suit by an int
ranging from 1 to 4, the concept of a card's suit doesn't relate to the numbers
1 to 4.
Furthermore, because there are a lot more legal integer values than valid suits, it's easy to assign to the variable representing the suit an invalid value, thus corrupting the object.
Instead, we can create meaningful types, like Suit
, which clearly represent
a concept from the problem domain, and can later be modified if needed.
It is generally a poor design choice to rely on primitive types (e.g., int
,
boolean
, and including String
) to represent unrelated concepts.
For example, while it might be tempting to represent a Suit by an int
ranging from 1 to 4, the concept of a card's suit doesn't relate to the numbers
1 to 4.
Furthermore, because there are a lot more legal integer values than valid suits, it's easy to assign to the variable representing the suit an invalid value, thus corrupting the object.
Instead, we can create meaningful types, like Suit
, which clearly represent
a concept from the problem domain, and can later be modified if needed.
Values of an enum are unique: two objects of the same enum type are equal if and only if they are the same object. In other words, two references are equal if they point to the same memory allocation.
As a result, we can compare variables of an enum type using the ==
operator,
rather than the Object.equals(Object)
method, although the latter would also
work.
Values of an enum are unique: two objects of the same enum type are equal if and only if they are the same object. In other words, two references are equal if they point to the same memory allocation.
As a result, we can compare variables of an enum type using the ==
operator,
rather than the Object.equals(Object)
method, although the latter would also
work.
We can be sure that there won't be any other instance of the enum because:
final
) and are static
;We can be sure that there won't be any other instance of the enum because:
final
) and are static
;The type Suit
represents, naturally, the suit of a playing card. Because there
are only 4 suits in a standard 52-card deck, Java's enum is a good option to
represent this concept.
The type Suit
represents, naturally, the suit of a playing card. Because there
are only 4 suits in a standard 52-card deck, Java's enum is a good option to
represent this concept.
The list of values of an enum, i.e., all possible instances, must be declared at the top of the enum declaration. In this case, there are only 4 values.
As common with constants (i.e., static final
fields), they are declared in
all uppercase, using underscores to split words.
The list of values of an enum, i.e., all possible instances, must be declared at the top of the enum declaration. In this case, there are only 4 values.
As common with constants (i.e., static final
fields), they are declared in
all uppercase, using underscores to split words.
In Java, an enum
, short for "enumerated type", is a special kind of class that
defines a finite set of constants, which are
of this class. These constants are visible anywhere in the code (i.e., public
visibility).
An enum is useful to represent abstractions that have a finite number of realizations, which are known in advance.
Enums are also a good way to avoid the antipattern.
In Java, an enum
, short for "enumerated type", is a special kind of class that
defines a finite set of constants, which are
of this class. These constants are visible anywhere in the code (i.e., public
visibility).
An enum is useful to represent abstractions that have a finite number of realizations, which are known in advance.
Enums are also a good way to avoid the antipattern.