0

Is there a way to use the "values()" method from a generic enum ?

public class TCP_Message<T extends Enum<T>> {

    public T anEnum;

    public void doSomething() {
        anEnum = T.values().values[0];
    }
}

this code does not allow me to access .values().

5
  • 1
    No. You'll need to provide the class of the enum. Commented Sep 11, 2017 at 17:18
  • stackoverflow.com/questions/3437897/… Commented Sep 11, 2017 at 17:19
  • 3
    Possibly related Iterate enum values using java generics Commented Sep 11, 2017 at 17:19
  • 1
    If you have the Class object of the enum -- as you'll probably need -- you can use getEnumConstants(). Commented Sep 11, 2017 at 17:22
  • Indeed, thanks for your answers, it works fine now. Commented Sep 11, 2017 at 18:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.