Smuggling information to enums

D

Daniel Pitts

Roedy said:
What the techniques are there for smuggling information to enums?

they don't have constructors. It is not clear how you would make
them nested classes.
enums are singletons, they do have constructors, but they are called
only once per classloader that loads them.
Is it possible to somehow have two enum class objects in RAM at once,
each with different instance data? see above.

Do you have to pass it in each time with the method or use static
setters?
That is one approach. It is called the flyweight pattern. I wrote a blog
about it a while ago.

<http://virtualinfinity.net/wordpress/program-design/2007/10/22/using-enums-as-a-flyweight-pattern/>

Enums are full classes. They can have constructors, and they can have
instance data, and they can have methods.

public enum MyEnum {
A("Test"),
B('c', 42, "Chicken") {
public void blah() {
System.out.println("blurp");
}
}
;
public final String name;
public final char answer;
public final int answer2;
public final String taste;

MyEnum(String name) {
taste = "Water";
answer = 'a';
answer2 = -1;
this.name = name;
}
MyEnum(char theAnswerIsAlways, int theAnswerToEverything,
String tasteLike) {
name = "Answers";
taste = tasteLike;
answer = theAnswerIsAlways;
answer2 = theAnswerToEverything;
}
public void blah() {
System.out.println("Blah!");
}
}

public class Test {
public static void main(String...args) {
MyEnum val = MyEnum.A;
System.out.println(val.name);
System.out.println(val.answer);
System.out.println(val.answer2);
System.out.println(val.taste);
val.blah();

val = MyEnum.B;
System.out.println(val.name);
System.out.println(val.answer);
System.out.println(val.answer2);
System.out.println(val.taste);
val.blah();
}
}

Output:
Test
a
-1
Water
Blah!
Answers
c
42
Chicken
blurp
 
M

Maarten Bodewes

Roedy said:
But there is still only one instance of the enum. If you change it, I
change it for all clients of the enum. That is really the problem.
Thanks for finally making that clear.


In case it is still not clear: enum instances should not (ever) be used
like normal class instances. The *purpose* of an enum is to be unique.
In your case, it might be a good idea to have an (inner?) class
definition with an enum instance as field. Then you may create a getter
for the enum so you can use switch statements on the embedded enum. The
variance is within other parts of the class. A slightly uglier thing to
do is to use only the enum in the class.equals() method.

I've created some Java 5 encoders/decoder streams (base64, yyencode
etc). I've never used enums to have anything to do with the characters,
but I did use them for my state machine. Very neat to use enums for that
kind of purpose. And you could create an enum for representing state
that implements an isFinal() or isFatal() method to indicate to the
inner loop that it's supposed to quit.

enums are neat, but don't abuse them. Good enums only carry compile time
information, not runtime information like you are trying to do.

Regards,
Maarten
 

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

Similar Threads

JDK 1.6.0_13 is out 6
Class-inheritance icon 1
ReadyBoost 12
Loving Enums 2
scope rules in enums 4
Dual View Enums 0
How to prevent code repetition with enums? 5
In praise of Java 1.5 enums 25

Members online

No members online now.

Forum statistics

Threads
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top