Java Reference
In-Depth Information
remedy: enumerated types. An enumerated type has a finite set of values, for
example
public enum FilingStatus {SINGLE, MARRIED}
You can have any number of values, but you must include them all in the enum
declaration.
You can declare variables of the enumerated type:
FilingStatus status = FilingStatus.SINGLE;
If you try to assign a value that isn't a FilingStatus , such as 2 or ÑSÑ , then
the compiler reports an error.
Use the == operator to compare enumerated values, for example:
if (status = = FilingStatus.SINGLE) . . .
It is common to nest an enum declaration inside a class, such as
public class TaxReturn
{
public TaxReturn(double anIncome,
FilingStatus aStatus) {. . .}
. . .
public enum FilingStatus SINGLE, MARRIED
private FilingStatus status;
}
To access the enumeration outside the class in which it is defined, use the class
name as a prefix:
TaxReturn return = new TaxReturn(income,
TaxReturn.FilingStatus.SINGLE);
An enumerated type variable can be null . For example, the status field in
the previous example can actually have three values: SINGLE, MARRIED , and
null . This can be useful, for example to identify an uninitialized variable, or a
potential pitfall.
Search WWH ::




Custom Search