|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 6
The mind-boggling part of this class definition is that the class uses itself to create new objects. The first time you reference RED, it doesn't exist. But the act of accessing the class that RED is
defined in causes it to be created, along with the other constants. Admittedly, that kind of recursive reference is rather
difficult to visualize. But the advantage is total type safety. A variable of type Color can never be assigned anything other
than the RED, GREEN, or BLUE objects that the Color class creates.
System.out.println(myColor);
Whenever you output an object to a character output stream like System.out, and whenever you concatenate an object to a string, Java automatically invokes the toString() method for that object. That's a good reason to define a toString() method for any new class you create.
If the class does not have a toString() method, the inheritance hierarchy is inspected until one is found. At the top of the hierarchy, the toString() method in the Object class returns the class name. So the toString() method always has some meaning, but most of the time the default method will not be very useful.
Here is a modification to the Color class that provides a useful toString() method:
public final class Color {
private String id;
private Color(String anID) {this.id = anID; }
public String toString() {return this.id; }
public static final Color RED = new Color("Red");
public static final Color GREEN = new Color("Green");
public static final Color BLUE = new Color("Blue");
}
This version adds a private String variable (id). The constructor has been modified to take a String argument and store it
as the object's ID. The toString() method then returns the object's ID.
One trick you can use to invoke the toString() method takes advantage of the fact that it is automatically invoked when an object is concatenated to a string. That means
you could put the object's name in a dialog by concatenating it to a null string using a line like the following:
textField1.setText("" + myColor);
Unless you happen to love all the parentheses in Lisp, you will find that a bit more readable than the alternative:
textField1.setText(myColor.toString());
It's also easier to make sure you put in the right number of closing parentheses!
Color class. The mechanism will be to assign an ordinal number to each class constant and reference it using the attribute .ord, like this: void placePiece(int location, int color) {
setPosition(location);
display(piece[color.ord]);
}
Although tacking on .ord to convert the reference to color into a number is not particularly pretty, it is not terribly obtrusive either. It seems like a fairly reasonable tradeoff
for typesafe constants.