I have two enums that cross reference each other. Each one has a constructor that has a parameter for other enum.

They look something like:

SchoolEnum(ImmuneEnum value)
{
   this.immune = value;
}

ImmuneEnum(SchoolEnum value)
{
   this.school = value;
}

However depending on which Enum I call first, I can get a null value for the reference variable.

ImmunityEnum immune = ImmunityEnum.IMMUNE_KANNIC; 
SchoolEnum school = SchoolEnum.KANNIC;
System.out.println(school.getImmune())
System.out.println(immune.getSchool());

Produces the output: null Kannic

SchoolEnum school = SchoolEnum.KANNIC;
ImmunityEnum immune = ImmunityEnum.IMMUNE_KANNIC; 
System.out.println(school.getImmune())
System.out.println(immune.getSchool());

Produces the output: immunekannic null

It seems to be a bit of the "chicken and egg" problem as to when the enum is instantiated. But is there a way to have each one properly reference the other? I am considering making two singleton hashmaps that artificially cross reference the two, but is there a better idea?

没有正确的解决方案

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top