final field of an enum to refer to later-named instance.

A

Andreas Leitgeb

[This is a supercede, just to correct a broken Subject]

I've got an enum that contains Things that come in pairs.

I'd like each Thing to have a *final* reference to
it's mate, that in half of the cases appears later
in the list of Thing-instances.

e.g.:
enum Thing {
T1, T2;
final Thing mate;
}

What could I do, to make T2 "final"ly the mate of T1
and vice versa ?

I know some alternatives, myself, like just making the
reference non-final (and initialize it in the enum's
static {...} block) or defining an abstract method in
the enum like "abstract Thing getMate();" and implement
it for each instance to return its mate...

Please only followup, if you know a trick for final fields,
or if you know that it is strictly impossible that way.
 
M

Mike Schilling

Andreas said:
[This is a supercede, just to correct a broken Subject]

I've got an enum that contains Things that come in pairs.

I'd like each Thing to have a *final* reference to
it's mate, that in half of the cases appears later
in the list of Thing-instances.

e.g.:
enum Thing {
T1, T2;
final Thing mate;
}

What could I do, to make T2 "final"ly the mate of T1
and vice versa ?

I know some alternatives, myself, like just making the
reference non-final (and initialize it in the enum's
static {...} block) or defining an abstract method in
the enum like "abstract Thing getMate();" and implement
it for each instance to return its mate...

Please only followup, if you know a trick for final fields,
or if you know that it is strictly impossible that way.

Not having played with enums, I don't know what games you can play with
their constructors, but this kind of thing will work for a "normal" class.
(The trick is to construct the mate if and only if it doesn't already
exist.)

import java.util.Map;
import java.util.HashMap;

public class Mates
{
public final Mates myMate;
private final int value;

private static Map<Integer, Mates> instances = new HashMap<Integer,
Mates>();

public Mates(int i)
{
value = i;
instances.put(i, this);
int mateValue = (i % 2 == 0) ? i + 1 : i - 1;
if (instances.get(mateValue) == null)
new Mates(mateValue);
myMate = instances.get(mateValue);
}

public String toString()
{
return Integer.valueOf(value).toString();
}


public static void main(String[] args)
{
Mates[] arr = new Mates[10];
for (int i = 0; i < 10; i++)
arr = new Mates(i);

for (int i = 0; i < 10; i++)
{
System.out.println(arr + " is the mate of " + arr.myMate);
}
}
}
 
A

Andreas Leitgeb

Mike Schilling said:
Not having played with enums, I don't know what games you can play with
their constructors,

Unfortunately, not enough :-(
The "next" enum-instance isn't even allocated, while the
current instance's constructor is not completed.

As it seems there really is no way.

In my case at hand, I've now changed my code to not really use
the enum-instances as such, but in the enum's static initializer
I create a few arrays, that are filled with return values of
method calls on the instances.
Later I do no longer touch the instances, but only read
the tables to get the infos I need.

Since the pairs were subsequent and I now use int's instead
of Thing-references, accessing the mate's results is now just
a matter of indexing with "i^1" instead of "i".

Thanks to all. Especially the trick for non-enums with
pair-wise creation (posted by a few) was new to me.

PS: the question about that "final" field was rather of
educational purpose. Even if it had worked, I'd have
switched to int-based processing at some time.

PPS: doing a switch on the index turned out to be noticably
faster than overriding an abstract method in the enum.
I really tried it, and wrote 40 anonymous subclasses for
my 40 instances. I still wouldn't claim that this always
needs to be the case. (Yes, I did the profiling first,
and it did indicate, that it would pay to optimize.)

PPPS: since an enum's .values() always does a .clone on it's
internally kept inventory-array ($VALUES), I also added a
static field:
private static final Thing things[]=Thing.values();
to help me get the Instance by index without cloning the
array each time. To my surprise I also didn't find a
method like valueOf(int).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,202
Latest member
MikoOslo

Latest Threads

Top