Java Reference
In-Depth Information
while (hi < MAX) {
System.out.println(hi);
hi = lo + hi;
lo = hi - lo;
}
}
}
Modifying the maximum value now requires only one change, in one part
of the program, and it is clearer what the loop condition is actually test-
ing.
You can group related constants within a class. For example, a card
game might use these constants:
class Suit {
final static int CLUBS = 1;
final static int DIAMONDS = 2;
final static int HEARTS = 3;
final static int SPADES = 4;
}
To refer to a
static
member of a class we use the name of the class
followed by dot and the name of the member. With the above declara-
tion, suits in a program would be accessed as
Suit.HEARTS
,
Suit.SPADES
,
and so on, thus grouping all the suit names within the single name
Suit
.
Notice that the order of the modifiers
final
and
static
makes no dif-
ferencethough you should use a consistent order. We have already ac-
cessed static fields in all of the preceding examples, as you may have
realized
out
is a
static
field of class
System
.
Groups of named constants, like the suits, can often be better repres-
ented as the members of an
enumeration type,
or
enum
for short. An
enum is a special class with predefined instances for each of the named