0

I have a class A that depends on a type parameter E, which I want to bound as an enum. This is what I did:

public class A<E extends Enum<E>> {}

But then I can't iterate over the elements of the enum. Here is an example:

public class A<E extends Enum<E>> {
    public void display() {
        for(E e : E.values())
            System.out.println(e);
    }
}

It doesn't work because it Cannot resolve method 'values' in 'E'.

What should I do instead to have the type parameter bounded to be an enum which can really behave like an enum?

Edit: All the solutions in the linked question are not applicable, since inside of the display method I don't have access to any concrete object of type E.

Matteo
  • 539
  • 3
  • 9
  • 2
    *"All the solutions in the linked question are not applicable inside of the display method I don't have access to any concrete object of type E"* - Well ... if that is true, then there is no solution to your problem. You need to rearrange your code so that *something* passes the actual `Class` object to this code. – Stephen C Aug 15 '21 at 07:32
  • 1
    Ask yourself, how would you do this without generics? If you cannot do it without generics, then you cannot do it with generics either, because the compiler erases generics code to non-generics code at compile-time, so if it can be done with generics, then it can also be done without generics. – user102008 Aug 27 '21 at 06:08

0 Answers0