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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:08:45+00:00 2026-06-13T20:08:45+00:00

I have written a Java enum where the values have various attributes. These attributes

  • 0

I have written a Java enum where the values have various attributes. These attributes could be stored in any of the following ways:

Using fields:

enum Eenum {
  V1(p1),
  V2(p2);

  private final A attr;

  public A attr() { return attr; }

  private Eenum(A attr) {
    this.attr = attr;
  }
}

Using abstract methods:

enum Eenum {
  V1 {
    public A attr() { return p1; }
  },

  V2 {
    public A attr() { return p2; }
  }

  public abstract A attr();
}

Using class level map:

enum Eenum {
  V1,
  V2;

  public A attr() { return attrs.get(this); }

  private static final Map<Eenum, A> attrs;

  static {
    ImmutableMap.Builder<Eenum, A> builder = ImmutableMap.builder();
    builder.put(V1, p1);
    builder.put(V2, p2);
    attrs = builder.build();
  }
}

How should I decide when to prefer which?

Thanks!

  • 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-13T20:08:47+00:00Added an answer on June 13, 2026 at 8:08 pm

    (I am answering my own question so that I can share some things I learned while trying out things.)

    Here are the questions you should ask to come at a decision for your specific case:

    1: Do the attribute values involve forward references?

    Sometimes V1‘s attribute may need a reference to V2 and vice versa. This is not a rare case. If you are dealing with such an enum, approach 1 simply would not work. The compiler will (rightly) complain about illegal forward references. Any of the other two approaches can be used.

    Now, if the attribute value is expensive to compute and a constant, you’d want that it’s computed only once. With approach 2, you’d have to introduce local variables per enum value, and cache results there. This is verbose but will give you better performance. With approach 3, the results are anyway computed only once, and so don’t have to do any extra work. This is more readable but somewhat less performant than approach 2. Design between these as per the specific trade offs warranted in your case.

    2: Do I need to cache results?

    Refer to the second paragraph of previous bullet.

    If there are no forward references, you can use approach 1 too. But if the computation involved in calculation of attributes is complex, you are better off with one of the other two approaches.

    3: Are the attributes relevant for all of the enum values?

    If not, then quite logically, you should be using a Map here. That is, approach 3.

    4: Are there any default values for some attributes for some enum values?

    If so, you can use all three approaches, and they all offer different set of trade-offs.

    With approach 1: You would define an auxiliary constructor that initializes the attribute to the default value. If there are multiple such attributes, this might not be a feasible approach.

    With approach 2: This will actually be like “fourth” approach Peter Lawrey suggested above. You will have a method returning the default value in enum‘s main body. And some enum values will override this method to return a different value. This is, again, quite verbose.

    With approach 3: Just less efficient. Good in every other way.

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

Sidebar

Related Questions

I have written a simple Java class to generate the hash values of the
I have written a Java UI using the SWT UI library and the MigLayout
I have written a Java desktop application in Windows platform that is using the
I have written a Java GUI using SWT. I package the application using an
I currently face the following problem: I have written a Java program that parses
I have written the following java code to connect the Oracle Express Edition database.....I
i have written a java code for updating the element value with the following
I have an Enum like this written in Java: public enum Status { ACTIVE(Active),
I have written a Java bytecode parser using Haskell, and it works just fine.
I have written java code for generating json of my searched data from file.But

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.