tags:

views:

42

answers:

3

I have an enum like:

    enum TEST {
        TEST1, TEST 2;

        public abstract <T> String stringify (T input);
    }

I need to add a constant specific method , something like stringify.

This method will take different types of inputs (for each enum). Can I do that? Eclipse is not letting me do it ..something like:

enum TEST {
    TEST1(
       public <Float> String stringify (Float input){
          return String.valueOf(input);
        }
    )
}
A: 

Think of each Enum value as a class. So yes, the enums can have methods, just like a class does -- they all have the same methods though.

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

Look at the Planet example.

Also note that the enum itself can have static methods....(just like a class)

hvgotcodes
I know that...but I want constant specific method with different types on each enum type. In this case if TEST1 is being called with Float, TEST2 might be called with Double.
gats
@gats, i think the best you can do, looking at your comment from the other answer, is have a static method on the enum itself and do something different based on the enum value.
hvgotcodes
A: 

No, you can't make each enum constant implement an abstract method but require a different type of input than the other enum constants. If you could, what would happen if you were given an instance of your TEST enum (you don't know what constant it is) and tried to call stringify on it? What type would you pass it?

Edit: Given what you've said about these enums being used to decode strings into objects, it seems to me you have several options:

  • You could get a String representation of each decoded object by just calling toString() on it.
  • You could add a set of overloaded static method on the enum class itself, called stringify(Float) and stringify(Double), etc. Then you could just call TEST.stringify(value) and if there were a stringify method for the value's type, it'd work fine.

I imagine there are other options as well.

ColinD
so the solution is to pass in Object as the parameter and in implementation, let each enum type cast the input to the type necessary ?
gats
@gats: Well... no. That wouldn't really be a good idea. Could you edit your question to explain what it is you want to accomplish by doing this? We might be able to suggest a better alternative.
ColinD
there is preexisting code in which enums are used to encode String values to a particular type...Now I need to add decode function...
gats
It is not always toString method. And using overloading I will have to write a lot of methods.
gats
@gats: You'd have to write a lot of methods if you were doing one per enum constant too, what's your point?
ColinD
A: 

You can't do it with enums, but you can simulate this behaviour with generic class:

public abstract class TEST<T> {
    public static final TEST<Float> TEST1 = new TEST<Float>() {
        public String stringify (Float input){   
            return String.valueOf(input);   
        }
    };

    public abstract <T> String stringify(T input);
}
axtavt