Thursday, October 28, 2010

Switch statement on enums

public enum Shape { RECTANGLE, CIRCLE, LINE }

The switch statement was enhanced to allow a convenient use of enums. Note that the case values don't have to be qualified with the enum class name, which can be determined from the switch control value.
switch (drawing) {
    case RECTANGLE: g.drawRect(x, y, width, height);
                    break;
    case CIRCLE   : g.drawOval(x, y, width, height);
                    break;
    case LINE     : g.drawLine(x, y, x + width, y + height);
                    break;
}

No comments:

Post a Comment

Chitika