8

I have enum that implement MyInterface. While making other class using that enum I want to constrain the enumClz to be class that has implemented MyInterface.

So I describe signature to be "T extends Enum< T extends MyInterface>" at generic type declaration.

public <T extends Enum< T extends MyInterface>> C1( Class<T> enumClz) {
    for (T anEnumConst : enumClz.getEnumConstants()) {
        //....process
    }
}

What surprised me is the IDE say it is "unexpected bound" at "T extends MyInterface". I don't know what it means by such two word error message, Any solution about this?


And by the way, out of curious I have an odd question though not really important. Can an enum type T be equivalent to the following infinite loop

<T extends Enum< T extends Enum<T extends<....>>>> ?

1
  • @alfasin extends when used in generic bounds means "is, or extends, or implements". Commented Oct 29, 2013 at 4:36

1 Answer 1

11

Declare the following instead:

public <T extends Enum<T> & MyInterface> C1(Class<T> enumClz)

Here, we're declaring T to have multiple upper bounds, which is possible for type parameters.

The declaration <T extends Enum<T extends MyInterface>> is invalid syntax because T must be bounded with a concrete type, but the T extends MyInterface in the type argument for Enum is trying to add more information about T when it's already been declared.

Note also that a class type must always come first when declaring multiple bounds. A declaration of <T extends MyInterface & Enum<T>> is also invalid syntax.

And by the way, out of curious I have an odd question though not really important. Can an enum type T be equivalent to the following infinite loop

<T extends Enum< T extends Enum<T extends<....>>>> ?

The declaration T extends Enum<T> is already "infinite" in that it's recursive. The same T that is being declared is given as a type argument for its upper bound - a type parameter's scope includes its own declaration.

More information:

Sign up to request clarification or add additional context in comments.

1 Comment

Is symbol & supported in java 6?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.