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

  • SEARCH
  • Home
  • 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 9174089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:41:49+00:00 2026-06-17T16:41:49+00:00

I have created an Enum to define certain actions. Programming against a external API

  • 0

I have created an Enum to define certain actions. Programming against a external API I am forced to use an Integer to express this action. That’s why I have added an integer instance field to my Enum. This should be d’accord with Joshua Bloch’s Effective Java, instead of relying on ordinal() or the order of the Enum constants using values()[index].

public enum Action {

    START(0),

    QUIT(1);

    public final int code;

    Protocol(int code) {
         this.code = code;
     }
}

I get an integer value what from the API and now I want to create an Enum value out of it, how can I implement this in the most generic fashion?

Obviously, adding such a factory method, will not work. You cannot instantiate an Enum.

Action valueOf(int what) {
     return new Action(what);
}

Of course, I can always make a switch-case statement and add all the possible codes and return the appropriate constant. But I want to avoid defining them in two places at the same time.

  • 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-06-17T16:41:50+00:00Added an answer on June 17, 2026 at 4:41 pm

    If you’re going to have a lot of them, you can use a HashMap<Integer, Action>:

    private static final Map<Integer, Action> actions = new HashMap<>(values().size, 1);
    
    static {
        for (Action action : values())
            actions.put(action.code, action);
    }
    
    // ...
    
    public static Action valueOf(int what) {
        return actions.get(what);
    }
    

    This is useful if you’re going to have a large number of Action values since the HashMap lookup is O(1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this macro code, which allows me to define both a C enum
I am trying to define a class. This is what I have: enum Tile
I have created an enum as so: enum Types { hi, hello, bye }
I have a web service in which I created an enum.. i have a
I have the following enum defined. I have used underscores as this enum is
I define enum this way: typedef enum sortByWhatUpperType{ //sortByRating=0, sortbyDistance=1, sortbyBuildingorProminent=0 } sortbyWhatBottomType; Then
Lets say I have this enum defined (from Apple's UIKit framework): typedef enum {
I have created an Enum and want to read the text value from it.
Let's assume that I have mytable in mySQL CREATE TABLE `mytable` ( `gender` enum('MALE','FEMALE','UNISEX')
I have created a QWidget with a bunch of QToolButtons in it and 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.