AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

constant values in a switch clause

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.developer
View previous topic :: View next topic  
Author Message
Wojtek Bok
Guest





PostPosted: Mon Jun 06, 2005 7:35 pm    Post subject: constant values in a switch clause Reply with 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".
Back to top
Oliver Wong
Guest





PostPosted: Thu Aug 11, 2005 2:49 pm    Post subject: Re: constant values in a switch clause Reply with quote




"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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.developer All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.