Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Tools / June 2005

Tip: Looking for answers? Try searching our database.

Constant values in switch/case

Thread view: 
Wojtek Bok - 08 Jun 2005 17:20 GMT
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".
James Korman - 09 Jun 2005 00:54 GMT
>If I set up a class definition with:
>
[quoted text clipped - 32 lines]
>the switch/case shows an error that "case expressions must be constant
>expressions".

Your assignment is legal. But doesn't qualify as a constant expression

See the LRM 15.28 Constant Expression

A compile-time constant expression is an expression denoting a value
of primitive type or a String that is composed using only the
following:
   ...
   * The unary operators +, -, ~, and ! (but not ++ or --)    <<<<
  ...

Jim
Wojtek Bok - 09 Jun 2005 16:05 GMT
>>------------------------------
>>private static int cvCounter = 0;
[quoted text clipped - 19 lines]
>     * The unary operators +, -, ~, and ! (but not ++ or --)    <<<<
>    ...

Ah ok, now I understand. The constant expression must be constant at
compile time, yet the values are not known until the class is first
instantiated.

Thanks.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.