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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:33:07+00:00 2026-06-09T02:33:07+00:00

I am trying to create a scenario where by an enum constant in enum

  • 0

I am trying to create a scenario where by an enum constant in enum class A has associated sub enum class B and enum class C containing their own constants. The constants in enum class B and enum class C group subsets of constants from enum class D. Below is a basically what I am trying to achieve:

enum A {
    CONST_1 ("const_1", B), // B is the associated enum
    CONST_2 ("const_2", C); // C in the associated enum

    private final String strVal;
    private final Enum associatedEnum;

    private A (String strVal, Enum associatedEnum) {
        this.strVal = strVal;
        this.associatedEnum = associatedEnum;
    }

    public Enum getAssociatedEnum() {
        return this.associatedEnum;
    }

    public String toString() {
        return this.strVal;
    }       

    // Associated Enum contained subset of grouped constants
    enum B {
        CONST_3 (D.CONST_7.toString()),
        CONST_4 (D.CONST_8.toString()); 

        private final String strVal;

        private B (String strVal) {
            this.strVal = strVal;
        }

        public String toString() {
            return this.strVal;
        }       
    }

    // Associated Enum contained subset of grouped constants
    enum C {
        CONST_5 (D.CONST_9.toString()),
        CONST_6 (D.CONST_10.toString()); 

        private final String strVal;

        private C (String strVal) {
            this.strVal = strVal;
        }

        public String toString() {
            return this.strVal;
        }              
    }
}

// Separate Enum containing all ungrouped constants
enum D {
    CONST_7 ("const_7"),
    CONST_8 ("const_8");
    CONST_9 ("const_9"),
    CONST_10 ("const_10");

    private final String strVal;

    private D (String strVal) {
        this.strVal = strVal;
    }

    public String toString() {
        return this.strVal;
    }    
}

Obviously this syntax doesn’t work OOTB because you cannot pass classes in Java this way. But can anyone suggest a way in which I could achieve this?

I am hoping to use it to validate static structured groupings in a client-side application.

  • 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-09T02:33:09+00:00Added an answer on June 9, 2026 at 2:33 am

    This should hopefully do what you want. I’ve included an example usage where it’ll list the sub enum type values.

    package a.b.c;
    
    
    public class EnumsTest {
    
      public enum A {
        A1( B.class ),
        A2( C.class );
    
        private final Class<? extends Enum<?>> enumClazz;
    
        A( final Class<? extends Enum<?>> enumClazz  ) {
          this.enumClazz = enumClazz;
        }
    
        public Enum<?>[] getSubEnumConstants() {
          return enumClazz.getEnumConstants();
        }
    
       /**
        * @param value
        * @return Never null
        * @throws IllegalArgumentException To be consistent with Enum.valueOf()
        */
       public <T> Enum<?> valueOfSubEnum( final String value ) throws IllegalArgumentException {
         for( Enum<?> enumInstance : getSubEnumConstants() ) {
           if( enumInstance.name().equals( value ) ) {
             return enumInstance;
           }
         }
         throw new IllegalArgumentException( "valueOf for " + enumClazz.getName() + " could not be resoled with the value of " + value );
       }
    
      }
    
      public enum B {
        B1,
        B2;
      }
    
      public enum C {
        C1,
        C2;
      }
    
      public static void main( String[] args ) {
    
        for( A a : A.values() ) {
          for( Enum<?> enumInstance : a.getSubEnumConstants() ) {
            System.out.println( a.name() + ":" +  enumInstance.name() );  
          }
        }
        Enum<?> valueOfSubEnum = A.A1.valueOfSubEnum( "B2" );
        System.out.println( valueOfSubEnum.name() );
      }
    }
    

    Note: If you want to lock the subtypes down to a specific set, you can make them implement an interface.

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

Sidebar

Related Questions

I am trying to create a generic formatter/parser combination. Example scenario: I have a
I am trying to create a little helper application, one scenario is file duplication
I am trying to create the following scenario... A user logs in by inserting
The scenario I am trying to create is to have my Silverlight Website on
I am trying to create a Django model for the following scenario: There are
I am trying to create a secure download web app with the following scenario.
I am trying to create a file containing a pkcs 7 block. In this
Using C++, I'm trying to create a generic container class to handle multiple data
I am trying to create a system which has a deterministic realtime response. I
I am trying to create a form which has a TextAreaFor email addresses. There

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.