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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:23:46+00:00 2026-05-23T07:23:46+00:00

I want to nest some enums. The object i’m representing are Flags, with a

  • 0

I want to nest some enums. The object i’m representing are Flags, with a type, and a value. There are a discrete number of types, and each type has a distinct set of possible values.

So if Type A can have values 1, 2 or 3, and Type B can have values 4,5,6, I’d like to be able to do things like:

Flag f = Flag.A.1;

f.getType() - returns "A"

f.getValue() - returns "1"

Flag f2 = Flag.A.4; -- Syntax error.

I’m driving myself crazy trying to nest enums within enums – is what i’m trying possible – do I need to ditch enums altogether and handcraft a static class with static members?

My best effort so far is:

public class Flag {

    enum A extends Flag {
        ONE("ONE"),
        TWO("TWO"),
        THREE("THREE");

        private A(String value) {
            Flag.type = "A";
            Flag.value = value;
        }
    }

        private static String type;
        private static String value;
}

But if I do:

Flag f = Flag.A.ONE;

The types are incompatible.

  • 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-23T07:23:47+00:00Added an answer on May 23, 2026 at 7:23 am

    You cannot have a number as an enum. It has to be an identifier.

    You can do this

    interface Flag {
        String getType();
        int getValue();
        enum A implements Flag{
            one, two, three;
            String getType() { return getClass().getSimpleName(); }
            int getvalue() { return ordinal()+1; }
        }
        enum B implements Flag{
            four, five, six;
            String getType() { return getClass().getSimpleName(); }
            int getvalue() { return ordinal()+4; }
        }
    }
    
    Flag f = Flag.A.one;
    

    However a simpler option may be

    enum Flag {
        A1, A2, A3, B4, B5, B6;
        public String getType() { return name().substring(0,1); }
        public int getValue() { return name().charAt(1) - '0'; }
    }
    
    Flag f = Flag.A1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

want to know why String behaves like value type while using ==. String s1
Possible duplicate: Populating HtmlTable Control with HtmlTable object in ASP/C# I want to nest
I'm trying to nest some routes under the namespace, account. I want user management
I want to nest if-else statements in Crystal Reports, but I don't know the
Would I want to nest one function into another?
I have 2 repos, A and B and I want to nest B inside
I want to nest a for loop inside a batch file to delete carriage
here is my sample class to why i want to nest. include(class.db.php); class Cart
I wrote some parser from combinatory library. I want a generic function that transform
I want to nest a element inside a form_for label tag. I want to

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.