 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Wojtek Bok Guest
|
Posted: Mon Jun 06, 2005 7:35 pm Post subject: constant values in a switch clause |
|
|
If I set up a class definition with:
------------------------------
public static final int SORT_FIRST_NAME = 0;
public static final int SORT_LAST_NAME = 1;
public static final int SORT_BIRTHDAY = 2;
------------------------------
then use this in a switch clause:
------------------------------
switch(sortOrder)
{
case SORT_FIRST_NAME:
// do work
case SORT_LAST_NAME:
// do work
case SORT_BIRTHDAY:
// do work
}
------------------------------
all is well. But, if I use dynamic allocation:
------------------------------
private static int cvCounter = 0;
public static final int SORT_FIRST_NAME = cvCounter++;
public static final int SORT_LAST_NAME = cvCounter++;
public static final int SORT_BIRTHDAY = cvCounter++;
------------------------------
then the switch/case statements show errors (in Eclipse 3.02).
Why? Eclipse accepts the value assignment. And the values ARE final, yet
the switch/case shows an error that "case expressions must be constant
expressions".
|
|
| Back to top |
|
 |
Oliver Wong Guest
|
Posted: Thu Aug 11, 2005 2:49 pm Post subject: Re: constant values in a switch clause |
|
|
"Wojtek Bok" <wb (AT) nospam (DOT) com> wrote
| Quote: | If I set up a class definition with:
------------------------------
public static final int SORT_FIRST_NAME = 0;
public static final int SORT_LAST_NAME = 1;
public static final int SORT_BIRTHDAY = 2;
------------------------------
then use this in a switch clause:
------------------------------
switch(sortOrder)
{
case SORT_FIRST_NAME:
// do work
case SORT_LAST_NAME:
// do work
case SORT_BIRTHDAY:
// do work
}
------------------------------
all is well. But, if I use dynamic allocation:
------------------------------
private static int cvCounter = 0;
public static final int SORT_FIRST_NAME = cvCounter++;
public static final int SORT_LAST_NAME = cvCounter++;
public static final int SORT_BIRTHDAY = cvCounter++;
------------------------------
then the switch/case statements show errors (in Eclipse 3.02).
Why? Eclipse accepts the value assignment. And the values ARE final, yet
the switch/case shows an error that "case expressions must be constant
expressions".
|
My guess is that the compiler must know the values of the CASE
statements at compile time. Since cvCounter is not final, it could be
written like this:
private static int cvCounter = Math.random();
public static final int SORT_FIRST_NAME = cvCounter++;
public static final int SORT_LAST_NAME = cvCounter++;
public static final int SORT_BIRTHDAY = cvCounter++;
In which case the compiler couldn't know the values of the CASE
statements at compile time. It's unfortunate, I know.
- Oliver
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|