 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
a Guest
|
Posted: Tue Aug 30, 2005 7:14 am Post subject: nested enum |
|
|
Is it possible to do a nested enum?
For example,
private enum inside0{a,b,c}
private enum inside1{d,e,f}
private enum outside{inside0, inside1}
outside _variable=outside.inside0.a; //error here, but want a similar
way
Is it possible to set the _variable in the way similar to the above?
Thanx
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Tue Aug 30, 2005 9:21 am Post subject: Re: nested enum |
|
|
On Tue, 30 Aug 2005 07:14:24 GMT, "a" <a (AT) mail (DOT) com> wrote or quoted :
| Quote: | For example,
private enum inside0{a,b,c}
private enum inside1{d,e,f}
private enum outside{inside0, inside1}
outside _variable=outside.inside0.a; //error here, but want a similar
way
Is it possible to set the _variable in the way similar to the above?
Thanx
|
enums are final. They are designed to be totally independent of any
other enum.
What you might do is something similar with nested classes to what
enum does, but in a way that lets you do what you want. I suggest
looking an decompilations of enum code.
See http://mindprod.com/jgloss/enum.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
|
|
| Back to top |
|
 |
Oliver Wong Guest
|
Posted: Tue Aug 30, 2005 2:45 pm Post subject: Re: nested enum |
|
|
"a" <a (AT) mail (DOT) com> wrote
| Quote: | Is it possible to do a nested enum?
For example,
private enum inside0{a,b,c}
private enum inside1{d,e,f}
private enum outside{inside0, inside1}
outside _variable=outside.inside0.a; //error here, but want a similar
way
Is it possible to set the _variable in the way similar to the above?
|
There's a fundamental misunderstanding about enums here. In the above
example, no nesting is occuring. In the outer namespace, you have an enum
called "inside0", and inside the namespace of the enum outside, you have an
instance called "inside0". The code above is similar to this:
public class inside0 {
public final inside0 a = new inside0();
public final inside0 b = new inside0();
public final inside0 c = new inside0();
}
public class inside1 {
public final inside1 d = new inside1();
public final inside1 e = new inside1();
public final inside1 f = new inside1();
}
public class outside {
public final outside inside0 = new outside();
public final outside inside1 = new outside();
}
As you can see from this code, you have a class named inside0, and you have
a field named inside0, and they each refer to distinct things.
- Oliver
|
|
| Back to top |
|
 |
Dale King Guest
|
Posted: Tue Sep 06, 2005 4:05 am Post subject: Re: nested enum |
|
|
a wrote:
| Quote: | Is it possible to do a nested enum?
For example,
private enum inside0{a,b,c}
private enum inside1{d,e,f}
private enum outside{inside0, inside1}
outside _variable=outside.inside0.a; //error here, but want a similar
way
Is it possible to set the _variable in the way similar to the above?
|
You can do nested enums in Java:
private enum outside
{
foo
public enum inside
{
bar
}
}
But what you describe is not a nested enum. This is really defining one
enum as the union of two other enums, which is not allowed in Java.
Perhaps what you really want is to define the enum as implementing an
interface (which could have the enums as nested enums:
public interface outside
{
public void someMethod();
public enum inside0 implements outside
{
a, b, c;
public void someMethod() { ... }
}
public enum inside1 implements outside
{
d, e, f;
public void someMethod() { ... }
}
}
--
Dale King
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|