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 6330915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:57:31+00:00 2026-05-24T17:57:31+00:00

Well.. it seems like enum classes in Java are a lot more versatile than

  • 0

Well.. it seems like enum classes in Java are a lot more versatile than their C or C++ counterpars, but for this specific code construct I’m trying to device their object nature is just giving me troubles.

What I have is something like this:

public static enum ServerResponse{
    SIGN_UP_SUCESS(0),
    INTERNAL_SERVER_ERROR(1),
    EMAIL_ALREADY_REGISTERED(2),
    SIGNIN_FAILED(3),
    WAITING_CONFIRMATION(4),
    SIGNIN_SUCESS(5),
    BLOCKED_ACCOUNT(6),
    INACTIVE_ACCOUNT(7);

    private final int numcode;
    ServerResponse(int num){
        numcode = num;
    }

    final int numCode(){ return numcode;}
}

You see the problem now arises as the server gives me a numeric response, which I cannot compare directly with the enum class. My idea was then to create the numCode() method that would return the integer property of the instantiated enum. I tried to do something like this:

int SERVER_RESPONSE = ServerInterface.sendRequest();

switch(SERVER_RESPONSE){
    ServerInterface.ServerResponse.BLOCKED_ACCOUNT.numCode():
        //Do something
    ServerInterface.ServerResponse.INTERNAL_SERVER_ERROR:
}

But as you can imagine none of the above worked. The first case comparison complains that “case expressions must be constant expressions” and the second type just gives me a type mismatch error.

So how should I do this? Well, right now I’m doing something like this:

 for(ServerResponse response : ServerResponse.values()){
        if(response.numCode() == SERVER_RESPONSE){
               return response;
        }
 }

But it’s ugly.. I would like to use a switch statement, that’s the whole purpose of enum types after all right?? So please, what am I missing here?

Thanks

Nelson

  • 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-24T17:57:34+00:00Added an answer on May 24, 2026 at 5:57 pm

    You’re missing a method in your enum which maps an integer to the enum value. Exactly how you do that is up to you – if your integers are contiguous starting with 0, you could potentially use an array… or you could use a Map<Integer, ServerResponse>. Either way, you can then do:

    // Please don't use caps for non-constants
    int serverResponseNumber = ServerInterface.sendRequest();
    ServerResponse serverResponse = ServerResponse.fromInt(serverResponseNumber);
    
    if (serverResponse == null) {
        // Do something with an unknown response - switch will barf otherwise.
        // Possibly throw an exception yourself?
    }
    switch(serverResponse) {
        case SIGN_UP_SUCCESS:
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it is completely straightforward, but my brain isn't working very well
This seems like a well known title, but I am really facing a problem
Well is there? From everything I've read, it seems like the answer is no,but
This seems like such a simple question, but I'm having such difficulty with it.
This question seems like it might be somewhat common, but I didn't find anything
This seems like such a trivial problem, but I can't seem to pin how
This seems like it should be simple but I can't find anything yet. In
This seems like it should be straight-forward, but I'm stumped. I have a link
Seems like this is a dumb question, but I need something... substantial. I have
Seems like I have seen how this is done in the past, but I

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.