I have an enum like the one below, but eclipse says that there are errors in the first definition of each opposite pair.
public enum Baz{
  yin(yang),    //Cannot reference a field before it is defined
  yang(yin),
  good(evil),   //Cannot reference a field before it is defined
  evil(good);
  public final Baz opposite;
  Baz(Baz opposite){
    this.opposite = opposite;
  }
}
What I want to accomplish is being able to use Baz.something.opposite to get the opposite object of Baz.something.  Is there a possible workaround for this?  Maybe an empty placeholder for yang and bad before yin and good are defined in this example?
You could try something like:
public enum Baz{
  yin("yang"),    
  yang("yin"),
  good("evil"),   
  evil("good");
  private String opposite;
  Baz(String opposite){
    this.opposite = opposite;
  }
  public Baz getOpposite(){
     return Baz.valueOf(opposite);
  }
}
and then reference it as
Baz.something.getOpposite()
That should accomplish what you are looking to do by looking up the enum value by it's string representation. I don't think you can get it to work with the recursive reference to Baz.
With the switch statement:
public enum Baz{
  yin,
  yang,
  good,
  evil;
  public Baz getOpposite() {
    switch (this) {
        case yin: return yang;
        case yang: return yin;
        case good: return evil;
        case evil: return good;
    }
    throw new AssertionError();
}
Or deferred initialization:
public enum Baz{
  yin,
  yang,
  good,
  evil;
  public Baz opposite;
  static {
    yin.opposite = yang;
    yang.opposite = yin;
    good.opposite = evil;
    evil.opposite = good;
  }
}
You might wish to make the mutable field private and provide a getter.
How about an EnumMap?
public enum Baz {
  yin,
  yang,
  good,
  evil;
  private static final Map<Baz, Baz> opposites = new EnumMap<Baz, Baz>(Baz.class);
  static {
    opposites.put(yin, yang);
    opposites.put(yang, yin);
    opposites.put(good, evil);
    opposites.put(evil, good);
  }
  public Baz getOpposite() {
    return opposites.get(this);
  }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With