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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:08:57+00:00 2026-05-25T11:08:57+00:00

I have a data structure in mind that involves nested enums, such that I

  • 0

I have a data structure in mind that involves nested enums, such that I could do something like the following:

Drink.COFFEE.getGroupName();
Drink.COFFEE.COLUMBIAN.getLabel();

And if there were method declarations:

someMethod(Drink type)
someOtherMethod(DrinkTypeInterface type)

Then I could say (appropriately):

someMethod(Drink.COFFEE)
someOtherMethod(Drink.COFFEE.COLUMBIAN)

This is what I came up with:

public enum Drink {

    COFFEE("Coffee");

    private String groupName;

    private Drink(String groupName) {
        this.groupName = groupName;
    }

    public enum Coffee implements DrinkTypeInterface {

        COLUMBIAN("Columbian Blend"),
        ETHIOPIAN("Ethiopian Blend");

        private String label;

        private Coffee(String label) {
            this.label = label;
        }

        public String getLabel() {
            return this.label;
        }
    }

    String getGroupName() {
        return this.groupName;
    }
}

And the interface:

public interface DrinkTypeInterface {

    public String getLabel();
}

I think I’m just trying to wrap my head around what the best way to do this sort of thing is in Java, or if I need to write a bunch of if-statements to deal with the individual Drink.values(). Any help?

  • 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-25T11:08:58+00:00Added an answer on May 25, 2026 at 11:08 am
    Drink.COFFEE.getGroupName();
    Drink.COFFEE.COLUMBIAN.getLabel();
    

    First off, that sample code you gave violates the "law of demeter" somewhat – as the COLUMBIAN instance field is only used to retrieve the label. Also, with that structure, COLUMBIAN has to be an instance of the COFFEE enum, but I don’t think that’s what you’re really going for here.

    someMethod(Drink type)
    someOtherMethod(DrinkTypeInterface type)
    
    someMethod(Drink.COFFEE)
    someOtherMethod(Drink.COFFEE.COLUMBIAN)
    

    What I’m gathering from what your sample is, is that you want to have an enumeration that contains a "group type" of what the actual drink is, and then each one has individual values for the specific type of drink. Your example gives Coffee, but Tea should work just as well.

    The problem is how you’ve placed your enumerations. As I said before, you’d have to make COLUMBIAN an INSTANCE of the COFFEE enumeration, but that’s not really the best way to structure this.

    The problem is that you’ve got Drink, then Coffee/Tea, and then their individual types.
    But, if you think about it, although HerbalTea IS A Tea, it is also a DRINK – so it doesn’t belong as simply an instance of a TEA.

    But, if you make the drink type an enum in and of itself, you get what you wanted, and the structure becomes clearer. And due to interfaces and the power of delegation, both the drink type and the drink enum can be processed in the same manner, as with the following example program:

    public final class DrinkEnumExample {
    
        public interface DrinkTypeInterface {
    
            String getDisplayableType();
        }
    
        public static enum DrinkType implements DrinkTypeInterface {
    
            COFFEE("Coffee"), TEA("Tea");
            private final String type;
    
            private DrinkType(final String type) {
                this.type = type;
            }
    
            public String getDisplayableType() {
                return type;
            }
        }
    
        public static enum Drink implements DrinkTypeInterface {
    
            COLUMBIAN("Columbian Blend", DrinkType.COFFEE),
            ETHIOPIAN("Ethiopian Blend", DrinkType.COFFEE),
            MINT_TEA("Mint", DrinkType.TEA),
            HERBAL_TEA("Herbal", DrinkType.TEA),
            EARL_GREY("Earl Grey", DrinkType.TEA);
            private final String label;
            private final DrinkType type;
    
            private Drink(String label, DrinkType type) {
                this.label = label;
                this.type = type;
            }
    
            public String getDisplayableType() {
                return type.getDisplayableType();
            }
    
            public String getLabel() {
                return label;
            }
        }
    
        public DrinkEnumExample() {
            super();
        }
    
        public static void main(String[] args) {
            System.out.println("All drink types");
            for (DrinkType type : DrinkType.values()) {
                displayType(type);
                System.out.println();
            }
            System.out.println("All drinks");
            for (Drink drink : Drink.values()) {
                displayDrink(drink);
                System.out.println();
            }
        }
    
        private static void displayDrink(Drink drink) {
            displayType(drink);
            System.out.print(" - ");
            System.out.print(drink.getLabel());
        }
    
        private static void displayType(DrinkTypeInterface displayable) {
            System.out.print(displayable.getDisplayableType());
        }
    }
    

    The output of this program is as follows:

    All drink types 
    Coffee 
    Tea 
    All drinks 
    Coffee - Columbian Blend 
    Coffee - Ethiopian Blend
    Tea - Mint 
    Tea - Herbal 
    Tea - Earl Grey
    

    Now then, if for some reason you didn’t want all your drinks in a single enum, then I didn’t understand what you were going for. In that case, if you do have functionality that spans the enums, make separate Coffee and Tea (and whatever) enumerations and apply the interface on both (or more) enumerations. But, I think you were trying to group them like this.

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

Sidebar

Related Questions

I have a data-structure (in plist) that looks something like this: What i have
I have a data structure that represents C# code like this: class Namespace: string
I have the following data structure (a list of lists) [ ['4', '21', '1',
I have a tree data structure that is L levels deep each node has
In Perl, when you have a nested data structure, it is permissible to omit
Assuming Visual C/C++ 6, I have a complex data structure of 22399 elements that
I have a design question. I have a nested repeater structure that is 4
I have a data structure which uses composite ids (Which I dont wish to
I have a data structure defined as struct myDataStruct { int32_t header; int16_t data[8];
I have a data structure where an entity has times stored as an int

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.