Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6168049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:37:08+00:00 2026-05-23T22:37:08+00:00

For my project I am using enums, and I need to implement the switch-case

  • 0

For my project I am using enums, and I need to implement the switch-case statement, where ordinal numbers of values of the specific Enum are checked, like this way:

        switch ( variable )
        {
        case MyEnum.A.ordinal():
            return true;
        case MyEnum.B.ordinal():
            return true;
        default:
            return false;
        }

Note: return values are only an example.

Unfortunately, Eclipse (I’m using 1.6 JDK) gives my compilation error “case
expressions must be constant expressions”. What I should do? Is there any other method than static lookup table, described here: Convert from enum ordinal to enum type ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:37:09+00:00Added an answer on May 23, 2026 at 10:37 pm

    This is how is done, provided you have a serialized ordinal somehow, somewhere. Usual way to persist an enum is by its name, not ordinal, though. Also you should not use ordinal in normal circumstances unless trying to implement something like EnumMap/Set. Of course, the enum can be just a port from C alike stuff and dealing with the inevitable int, needs a transform to the Enum object.

    Just use Enum.values() to obtain an array ordered by ordinal(), since the array is cloned each time, keeping a ref towards is ok.

    enum E{
     A, B, C...   
    }
    
    final static E[] vals = E.values();//copy the values(), calling values() clones the array
    boolean f(int variable){
      switch(vals[variable]){
      case A:
    ...
      case B:
    ...
    //break;
      default:
    ...
       }
    }
    

    Just noticed you need only true and false, that’s a Set type of behavior. You can use java.util.EnumSet or a simple long, if feeling brave (and not having more than 64 enum constants). for example:

    private static <E extends Enum> long ord(E e){
      return 1L<<e.ordinal();
    }
    
    static final long positiveSet = ord(E.A)+ord(E.B);
    boolean f(int ordinal){
      return 0!=(positiveSet&(1L<<ordinal));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my current project I'm using classes which implement the following ITransaction interface shown
I have several Enum types defined across my project, used for object-specific state identifiers:
Using AspectJ with enums I get the next results: public enum EnumName { B,
I've just started using Java's enums in my own projects (I have to use
I've been using a small class to emulate Enums in some Python projects. Is
I've got a project using Maven 2 as the build tool. Now I am
I have a test project using MbUnit and TestDriven.Net. If I right-click on an
I'm building a project using a GNU tool chain and everything works fine until
I am building a project using Visual Studio. The project has a dependency on
After compiling a simple C++ project using Visual Studio 2008 on vista, everything runs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.