From: Magnus Warker on
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
From: Peter Duniho on
Magnus Warker wrote:
> 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:
> [...]
> Have I missed something? How do you do that?

Have you thought about using enums instead? If enums aren't solving
your need, it would be helpful if you could explain why.

Pete
From: Lew on
Magnus Warker wrote:
> 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;

Those should have been 'final'.

> 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.

Strictly speaking, in Java terms those aren't "constants" but "final
variables". To be "constants", they'd have to be initialized with a
compile-time constant expression, per
<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#10931>
and
<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5313>

> 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?

Make 'Color' an enum.

--
Lew
From: Magnus Warker on
It's the readibility of the code.

With constant classes I can write something like this:

public void setColor (Color color)
{
if (color == Color.WHITE)
...
}

This is not possible with enums, since an enume type must be defined in a
class:

public void setColor (SomeClass.Color color)
{
if (color == SomeClass.Color.WHITE)
...
}

I think, it would be ok, if I needed the enum only in SomeClass.

But what about constants that are needed in many classes?

I saw that constant classes are used widely in the java api, e. g.
java.awt.PageAttributes.MediaType. But I also saw constants declared as
final ints anyway.

Thanks
Magnus

Peter Duniho wrote:
> Have you thought about using enums instead? If enums aren't solving
> your need, it would be helpful if you could explain why.

From: Alan Gutierrez on
Magnus Warker wrote:

> This is not possible with enums, since an enume type must be defined in a
> class:

Someone is about to pop in and give you chapter and verse of JLS that
tells you that's not so, but I'll just tell you that's not so.

--
Alan Gutierrez - alan(a)blogometer.com - http://twitter.com/bigeasy