GeeCON Prague 2014*
The moose likes Java in General and the fly likes Nesting Enums within Enums Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Nesting Enums within Enums" Watch "Nesting Enums within Enums" New topic
Author

Nesting Enums within Enums

John P Coffey
Greenhorn

Joined: Jul 02, 2010
Posts: 3
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


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

public enum FOOProtAttr {
ATTR1,
ATTR2,
ATTR3;
}

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);
}
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Please edit your post and UseCodeTags.

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



SCJP 6 || SCWCD 5
John P Coffey
Greenhorn

Joined: Jul 02, 2010
Posts: 3
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

Joined: Jun 17, 2009
Posts: 273

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

Joined: Oct 27, 2005
Posts: 19697
    
  20

Michael is right. In enums, nothing can come before the enum values.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
GeeCON Prague 2014
 
subject: Nesting Enums within Enums