Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nesting Enums within Enums

 
John P Coffey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code works but I would like to somehow be able to nest the FOOVariant and FOOProtAttr enumerated types within the FOOTypeMapping enum, does anyone know if this is possible? I'm not quite sure how to go about embedding those two enums

Thanks in advance
John

 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please edit your post and UseCodeTags.

Just put the enums inside FOOTypeMapping as inner-classes...

 
John P Coffey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael, thanks for the response, however if you try nesting the outer enums from the example I showed earlier it fails. i.e the code below fails - however as soon as I bring the FOOVariant and FOOProtAttr outside the outer FOOTypeMapping all compiles. The first error netbeans spits out says something about
[code]
<identifier> expected public enum FOOTypeMapping {
',', '}', or ';' expected
public enum FOOVariant {
[/code]

Thanks

John

[code]
public enum FOOTypeMapping {
public enum FOOVariant {
A,B,C,D;
};

public enum FOOProtAttr {
ATTR1,
ATTR2,
ATTR3;
};

FTM_A_ATTR1(FOOVariant.A, EnumSet.of(FOOProtAttr.ATTR1)),
FTM_B_ATTR23(FOOVariant.A, EnumSet.of(FOOProtAttr.ATTR2, FOOProtAttr.ATTR3));
private FOOTypeMapping(FOOVariant var, EnumSet<FOOProtAttr> attr) {
mAttrs.put(var, attr);
}
private final EnumMap<FOOVariant, EnumSet<FOOProtAttr>> mAttrs =
new EnumMap<FOOVariant, EnumSet<FOOProtAttr>>(FOOVariant.class);
}
[/code]
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must put the two inner Enums at the [i]end[/i] of the outer Enum. So like:

[code]
public enum FOOTypeMapping {
FTM_A_ATTR1(FOOVariant.A, EnumSet.of(FOOProtAttr.ATTR1)),
FTM_B_ATTR23(FOOVariant.A, EnumSet.of(FOOProtAttr.ATTR2, FOOProtAttr.ATTR3));
private FOOTypeMapping(FOOVariant var, EnumSet<FOOProtAttr> attr) {
mAttrs.put(var, attr);
}
private final EnumMap<FOOVariant, EnumSet<FOOProtAttr>> mAttrs =
new EnumMap<FOOVariant, EnumSet<FOOProtAttr>>(FOOVariant.class);

public enum FOOVariant {
A,B,C,D;
};

public enum FOOProtAttr {
ATTR1,
ATTR2,
ATTR3;
};
}[/code]
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael is right. In enums, nothing can come before the enum values.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic