0

I need to access the value of captions using enum as specified in below.

public enum Caption {

    //Object types
    //-------------
    Obj1                ("Obj1"), 
    Obj2                ("Obj2"), 
    Obj3                ("Obj3"), 
    Obj4                ("Obj4"), 


    //Menu Bar
    //-------------
    Menu1               ("Menu1"), 
    Menu2               ("Menu2"), 
    Menu3               ("Menu3"), 
    Menu4               ("Menu4"); 

    public String Value;

    Caption(String caption) {
        this.Value = caption;
    }

} //Caption

I am able to access this as Caption.Obj1.Value. Instead I would like to access as Caption.ObjectTypes.Obj1.Value, Caption.MenuBar.Menu1.Value.

Could you please suggest how to use java here to achieve this.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Deva
  • 21
  • 1
  • 2
  • 6

1 Answers1

0

You need a semicolon on the last Menu, and you could add a pair of static inner classes to do allow that syntax. Something like,

  public enum Caption {

    // Object types
    // -------------
    Obj1("Obj1"), Obj2("Obj2"), Obj3("Obj3"), Obj4("Obj4"),
    // Menu Bar
    // -------------
    Menu1("Menu1"), Menu2("Menu2"), Menu3("Menu3"), Menu4("Menu4");

    public String Value;

    Caption(String caption) {
      this.Value = caption;
    }

    public static class ObjectTypes {
      public static final Caption Obj1 = Caption.Obj1;
      public static final Caption Obj2 = Caption.Obj2;
      public static final Caption Obj3 = Caption.Obj3;
      public static final Caption Obj4 = Caption.Obj4;
    }

    public static class MenuBar {
      public static final Caption Menu1 = Caption.Menu1;
      public static final Caption Menu2 = Caption.Menu2;
      public static final Caption Menu3 = Caption.Menu3;
      public static final Caption Menu4 = Caption.Menu4;
    }
  } // Caption
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Interesting approach. But I think nesting enums like in Patrick Perini's answer here http://stackoverflow.com/questions/8732710/enum-within-an-enum would be more elegant. I am at a tablet atm and cannot say for sure it works tho. – Gee858eeG Jan 22 '16 at 03:26