0

I want to define few String constants inside an Enum value and access these constant in context with in same enum value. For e.g. Below is my enum definition:

public enum EventType {
    QUERY_UPDATE {
        public String QUERY_LIST = "QUERY_LIST"; //this variable is not accessible in client code.
    },
    SINGLE_RESULT_HANDLE {
        public static final String SINGLE_QUERY_OBJECT = "SINGLE_QUERY_OBJECT"; //this variable is not accessible in client code.
        public static final String SINGLE_QUERY_RESULT = "SINGLE_QUERY_RESULT";
    },
    COLLATED_RESULT_HANDLE {
         enum Keys{ //compiler error : The member enum Keys can only be defined inside a top-level class or interface or in a static context.
            COLLATED_RESULT_LIST,
        }
    };
}

In my code, where I want to use these enum values :

//client code.

GenericEvent resultEvent = new GenericEvent(EventType.QUERY_UPDATE);
resultEvent.addContextProperty(EventType.QUERY_UPDATE.QUERY_LIST, "abcd"); //LINE 2

Now in LINE 2 (first argument) above, I want to specify the String Constant which belongs only to EventType.QUERY_UPDATE. But there seems no way to get this done. Please suggest how to resolve this.

Amit Parashar
  • 1,447
  • 12
  • 15
  • 3
    Similar questions have been asked before, such as [here](http://stackoverflow.com/questions/8732710/enum-within-an-enum), and [here](http://stackoverflow.com/questions/8069929/enum-within-enum). The answer to your question will be to not nest an enum inside an enum but rather to come up with an alternative design. – Tim Biegeleisen Jan 20 '16 at 07:34
  • Thanks @TimBiegeleisen , The design change will not be tidy.I wanted to avoid several implementations. Also IDE's like eclipse support quick listing of Enum values. (Ctrl + Enter). – Amit Parashar Jan 20 '16 at 07:50
  • Could you update your question with some background about the business problem you are trying to solve? There is definitely a way to architect what you what, but giving only a enum definition makes that difficult. – Tim Biegeleisen Jan 20 '16 at 07:53
  • you can move the keys declaration outside the COLLATED_RESULT_HANDLE constant, but inside the EventType enum. – Ray Tayek Aug 05 '19 at 01:47

0 Answers0