Java Reference
In-Depth Information
TIP
A common technique used with public static final constants is to put
them in an interface definition. (This is the exception to the rule that interfaces
define method signatures but contain no data.) When a class wants to use one
or more of those constants, it is declared to implement that interface:
public MyClass
extends BigClass
implements Comparable, LotsaConstants
{
...
}
In defining MyClass we have declared that it implements LotsaConstants
(not a name that we recommend you using). That makes all the constants that
we have defined inside the LotsaConstants interface available to the
MyClass class. Since classes can implement many different interfaces, this
doesn't interfere with the use of other “real” interfaces, such as Comparable .
WARNING
The keyword enum is new to Java 5.0, so older programs that may have used
enum as a variable name and will now cause an error when recompiled for
Java 5.0.
The enum will look very familiar to C/C++ programmers, but there are
some important differences. In C/C++ the values of the enum elements are, in
reality, integer values. Not so in Java. Here they are their own type, but can be
converted to a String via the toString() method, with a value that matches
the name, for easy reading and debugging.
Enumerated values can be used in == comparisons since they will be
defined only once (like other static final constants) and it would only be
references that are passed around. They would be referenced by the name of
the enumeration followed by dot followed by the particular value (e.g.,
WallMods.WINDOW ) and used as an object. (We have used uppercase for the
names not out of any syntax requirement, but only to follow the typical naming
convention for constants.)
Search WWH ::




Custom Search