M
Magnus Warker
Hi,
in the past I used to declare my constants as this, e. g. the colors of a
chess board:
public static int COL_WHITE = 1;
public static int COL_BLACK = 2;
Now, I find class constants very useful:
public final class Color
{
public static final Color WHITE = new Color();
public static final Color BLACK = new Color();
....
}
The major advantage for me is that I can use the constants over the classes
name, e. g. Color.WHITE.
However, I found that I lose the ability to evaluate these constants in
switch statements:
switch(color)
{
case Color.WHITE:
...
Instead, I have to use cascading if-statements:
if (color==Color.WHITE)
{
}
else
if (color==Color.WHITE)
{
}
else
...
This is a major drawback in my opinion.
Have I missed something? How do you do that?
Thanks
Magnus
in the past I used to declare my constants as this, e. g. the colors of a
chess board:
public static int COL_WHITE = 1;
public static int COL_BLACK = 2;
Now, I find class constants very useful:
public final class Color
{
public static final Color WHITE = new Color();
public static final Color BLACK = new Color();
....
}
The major advantage for me is that I can use the constants over the classes
name, e. g. Color.WHITE.
However, I found that I lose the ability to evaluate these constants in
switch statements:
switch(color)
{
case Color.WHITE:
...
Instead, I have to use cascading if-statements:
if (color==Color.WHITE)
{
}
else
if (color==Color.WHITE)
{
}
else
...
This is a major drawback in my opinion.
Have I missed something? How do you do that?
Thanks
Magnus