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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:16:21+00:00 2026-05-20T02:16:21+00:00

When working with variables/parameters that can only take a finite number of values, I

  • 0

When working with variables/parameters that can only take a finite number of values, I try to always use Java’s enum, as in

public enum BonusType {
  MONTHLY, YEARLY, ONE_OFF
}

As long as I stay inside my code, that works fine. However, I often need to interface with other code that uses plain int (or String) values for the same purpose, or I need to read/write from/to a database where the data is stored as a number or string.

In that case, I’d like to have a convenient way to associate each enum value with a an integer, such that I can convert both ways (in other words, I need a “reversible enum”).

Going from enum to int is easy:

public enum BonusType {
  public final int id;

  BonusType(int id) {
    this.id = id;
  }
  MONTHLY(1), YEARLY(2), ONE_OFF(3);
}

Then I can access the int value as BonusType x = MONTHLY; int id = x.id;.

However, I can see no nice way for the reverse, i.e. going from int to enum. Ideally, something like

BonusType bt = BonusType.getById(2); 

The only solutions I could come up with are:

  • Put a lookup method into the enum, which uses BonusType.values() to fill a map “int -> enum”, then caches that and uses it for lookups. Would work, but I’d have to copy this method identically into each enum I use :-(.
  • Put the lookup method into a static utility class. Then I’d only need one “lookup” method, but I’d have to fiddle with reflection to get it to work for an arbitrary enum.

Both methods seem terribly awkward for such a simple (?) problem.

Any other ideas/insights?

  • 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-20T02:16:22+00:00Added an answer on May 20, 2026 at 2:16 am

    http://www.javaspecialists.co.za/archive/Issue113.html

    The solution starts out similar to yours with an int value as part of the enum definition. He then goes on to create a generics-based lookup utility:

    public class ReverseEnumMap<V extends Enum<V> & EnumConverter> {
        private Map<Byte, V> map = new HashMap<Byte, V>();
        public ReverseEnumMap(Class<V> valueType) {
            for (V v : valueType.getEnumConstants()) {
                map.put(v.convert(), v);
            }
        }
    
        public V get(byte num) {
            return map.get(num);
        }
    }
    

    This solution is nice and doesn’t require ‘fiddling with reflection’ because it’s based on the fact that all enum types implicitly inherit the Enum interface.

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

Sidebar

Related Questions

I am working in java and wanting to use something like output parameters. Here
The code that I am working on is changing my temporary variables, and I
I need a function that takes four unsigned char variables as parameters and combines
HashMap internally has its own static final variables for its working. static final int
For a Magento shop I'm working on I have to check certain session variables
Hai am trying to load the variables from parent(flash) to child(flash).Its working fine,. parent
I am working with a Drupal theme, and I see a lot of variables
Possible Duplicate: Where are $_SESSION variables stored? When ever we working with session, we
For homework, I'm working with the following three classes. Class | Extends | Variables
I have an SQL query that takes these parameters: @SearchFor nvarchar(200) = null ,@SearchInLat

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.