Search code examples
javagenericsboundstype-parameter

In Java, Type Variables' bounds can only be present in the Type Variable declaration, right?


Type Variables' bounds can only appear in the declaration of classes, interface, methods and constructors?

Or can I bound a type variable when they are used as type arguments?

Edit: Example:

class MyClass<T extends Number> { // T is bounded by the interface Number
    // can a bounded Type Parameter appear anywhere else,
    // besides the Type parameter declaration?
}

Solution

  • The Java Language Specification seems to agree with you:

    A type variable (§4.4) is an unqualified identifier. Type variables are introduced by generic class declarations (§8.1.2) generic interface declarations (§9.1.2) generic method declarations (§8.4.4) and by generic constructor declarations (§8.8.4).