Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Enums: Two enum types, each containing references to each other?

Is there a way to get around the class-loading issues caused by having two enums that reference each other?

I have two sets of enumerations, Foo and Bar, defined like so:

public class EnumTest {    public enum Foo {     A(Bar.Alpha),     B(Bar.Delta),     C(Bar.Alpha);      private Foo(Bar b) {       this.b = b;     }      public final Bar b;   }    public enum Bar {     Alpha(Foo.A),     Beta(Foo.C),     Delta(Foo.C);      private Bar(Foo f) {       this.f = f;     }      public final Foo f;   }    public static void main (String[] args) {     for (Foo f: Foo.values()) {       System.out.println(f + " bar " + f.b);     }     for (Bar b: Bar.values()) {       System.out.println(b + " foo " + b.f);     }   } } 

The above code produces as output:

A bar Alpha B bar Delta C bar Alpha Alpha foo null Beta foo null Delta foo null 

I understand why it happens - the JVM starts classloading Foo; it sees the Bar.Alpha in Foo.A's constructor, so it starts classloading Bar. It sees the Foo.A reference in the call to Bar.Alpha's constructor, but (since we're still in Foo.A's constructor) Foo.A is null at this point, so Bar.Alpha's constructor gets passed a null. If I reverse the two for loops (or otherwise reference Bar before Foo), the output changes so that Bar's values are all correct, but Foo's values are not.

Is there any way to get around this? I know I can create a static Map and a static Map in a 3rd class, but that feels fairly hackish to me. I could also make Foo.getBar() and Bar.getFoo() methods that refer to the external map, so it wouldn't even change my interface (the actual classes I have use inspectors instead of public fields), but it still feels kind of unclean to me.

(The reason I'm doing this in my actual system: Foo and Bar represent types of messages that 2 apps send to each other; the Foo.b and Bar.f fields represent the expected response type for a given message - so in my sample code, when app_1 receives a Foo.A, it needs to reply with a Bar.Alpha and vice-versa.)

Thanks in advance!

like image 517
Sbodd Avatar asked Oct 01 '09 20:10

Sbodd


People also ask

Can enum reference another enum?

Enum implicitly and therefore cannot extend another class. This means extensibility for enums most be derived from a different source. This is about as close as you can get to shared state when it comes to enums. As Johan Sjöberg said above, it might just be easiest to simply combine the enums into another enum.

Can enum have different types?

I must answer a resounding no because actually you can't. Enums have their own data type and each enum is essentially a new data type.

Can two different enums have the same value?

1. Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.

Are Java enums reference types?

Enums are reference types, in that they can have methods and can be executed from command line as well , if they have main method.


1 Answers

One of the best ways would be using the enum polymorphism technique:

public class EnumTest {     public enum Foo {         A {              @Override             public Bar getBar() {                 return Bar.Alpha;             }         },         B {              @Override             public Bar getBar() {                 return Bar.Delta;             }         },         C {              @Override             public Bar getBar() {                 return Bar.Alpha;             }         },          ;          public abstract Bar getBar();     }      public enum Bar {         Alpha {              @Override             public Foo getFoo() {                 return Foo.A;             }         },         Beta {              @Override             public Foo getFoo() {                 return Foo.C;             }         },         Delta {              @Override             public Foo getFoo() {                 return Foo.C;             }         },          ;          public abstract Foo getFoo();     }      public static void main(String[] args) {         for (Foo f : Foo.values()) {             System.out.println(f + " bar " + f.getBar());         }         for (Bar b : Bar.values()) {             System.out.println(b + " foo " + b.getFoo());         }     } } 

The above code produces the output you want:

A bar Alpha B bar Delta C bar Alpha Alpha foo A Beta foo C Delta foo C 

See also:

  • Overridding method of a specific enum
like image 110
falsarella Avatar answered Sep 21 '22 20:09

falsarella