1

I have two Enums. First:

public enum FirstEnum {

    FIRST(SECOND);

    private SecondEnum secondEnum;

    FirstEnum(SecondEnum secondEnum) {
        this.secondEnum = secondEnum;
    }

    public SecondEnum getSecondEnum() {
        return secondEnum;
    }
}

And second enum:

 public enum SecondEnum {

    SECOND(FIRST);

    private FirstEnum firstEnum;

    SecondEnum(FirstEnum firstEnum) {
        this.firstEnum = firstEnum;
    }

    public FirstEnum getFirstEnum() {
        return firstEnum;
    }
}

When i run this code:

public static void main(String[] args) {
    System.out.println("First has: " + FIRST.getSecondEnum());
    System.out.println("Second has: " + SECOND.getFirstEnum());
}

I have this result:

First has: SECOND
Second has: null

If i change the lines - it will be vice versa. I understand that there is may be some like deadlock, but what can i do if i really need this kind of relationship?

Adeptius
  • 598
  • 5
  • 7

0 Answers0