Joshua Cranmer <Pidgeot18@verizon.invalid> wrote:
Andreas Leitgeb wrote:
enum Thing {
T1, T2;
final Thing mate;
}
What could I do, to make T2 "final"ly the mate of T1
and vice versa ?
Strictly speaking, having each store a final Object reference to each
other is impossible, since a final variable is only set at construction,
and an object has to be created before it can be referred to. [1]
Hmm, damn, I feared so :-(
1. Make the mate a String. Because of the way enums work, you get the
same effect if you do |Thing.valueOf(mate)|.
Yes, String, or index into values()...
2. If you drop finality, you can still initialize in the constructor.
Ah, yes, that's an alternative that occurred to me after writing my
post. I also hoped that javac could be tricked into initializing another
Thing's final ref as well as this's, but that (of course) didn't work.
3. A less agreeable one, IMO. You can use some form of static-time
initialization, either manually setting the variables (ew?) or using a
map of some sort.
That's my current fallback-approach. In my case, the index of each
Thing's mate can be calculated from Thing's ordinal(), and I've already
got a loop over all Things in the static initializer, so I just non-
final'ed the ref and added initialization of that ref into the loop.
4. As you mentioned, you can also do abstract methods, but that seems
rather burdensome for such a feature.
Indeed :-) With my 40 Thing-instances, it would also be very verbose.
I could think of a few changes in the implementation of enum-
initialization that would allow for forward-references, but I
guess, forward-references among enum-instances are not considered
valuable enough to justify any changes at all.
configuration of mate.