---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 24 Edition"
java_version: 24
chapter: "Chapter 8. Classes"
section: "8.1.2"
heading: "8.1.2. Generic Classes and Type Parameters"
parent: "8.1. Class Declarations"
source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-8.html#jls-8.1.2
id: "8.1.2"
url: https://jvm.obfuscated.software/jls/se24/8.1.2.html
raw: https://jvm.obfuscated.software/jls/se24/8.1.2.md
---

# 8.1.2. Generic Classes and Type Parameters

A class is *generic* if the class declaration declares one or more type variables ([§4.4](4.4.md)).

These type variables are known as the *type parameters* of the class. The type parameter section follows the class name and is delimited by angle brackets.

```
TypeParameters:
  < TypeParameterList >

TypeParameterList:
  TypeParameter {, TypeParameter}
```

The following productions from [§4.4](4.4.md) are shown here for convenience:

```
TypeParameter:
  {TypeParameterModifier} TypeIdentifier [TypeBound]

TypeParameterModifier:
  Annotation

TypeBound:
  extends TypeVariable
  extends ClassOrInterfaceType {AdditionalBound}

AdditionalBound:
  & InterfaceType
```

The rules concerning annotation modifiers for a type parameter declaration are specified in [§9.7.4](9.7.4.md) and [§9.7.5](9.7.5.md).

In a class's type parameter section, a type variable T *directly depends* on a type variable S if S is the bound of T, while T *depends* on S if either T directly depends on S or T directly depends on a type variable U that depends on S (using this definition recursively).

It is a compile-time error if a type variable in a class's type parameter section depends on itself.

The scope and shadowing of a class's type parameter is specified in [§6.3](6.3.md) and [§6.4.1](6.4.1.md).

References to a class's type parameter from a static context or a nested class or interface are restricted, as specified in [§6.5.5.1](6.5.5.1.md).

A generic class declaration defines a set of parameterized types ([§4.5](4.5.md)), one for each possible parameterization of the type parameter section by type arguments. All of these parameterized types share the same class at run time.

For instance, executing the code:

```
Vector<String>  x = new Vector<String>();
Vector<Integer> y = new Vector<Integer>();
boolean b = x.getClass() == y.getClass();
```

will result in the variable `b` holding the value `true`.

It is a compile-time error if a generic class is a direct or indirect subclass of `Throwable` ([§11.1.1](11.1.1.md)).

This restriction is needed since the catch mechanism of the Java Virtual Machine works only with non-generic classes.

**Example 8.1.2-1. Mutually Recursive Type Variable Bounds**

```
interface ConvertibleTo<T> {
    T convert();
}
class ReprChange<T extends ConvertibleTo<S>,
                 S extends ConvertibleTo<T>> {
    T t;
    void set(S s) { t = s.convert();    }
    S get()       { return t.convert(); }
}
```

**Example 8.1.2-2. Nested Generic Classes**

```
class Seq<T> {
    T      head;
    Seq<T> tail;

    Seq() { this(null, null); }
    Seq(T head, Seq<T> tail) {
        this.head = head;
        this.tail = tail;
    }
    boolean isEmpty() { return tail == null; }

    class Zipper<S> {
        Seq<Pair<T,S>> zip(Seq<S> that) {
            if (isEmpty() || that.isEmpty()) {
                return new Seq<Pair<T,S>>();
            } else {
                Seq<T>.Zipper<S> tailZipper =
                    tail.new Zipper<S>();
                return new Seq<Pair<T,S>>(
                    new Pair<T,S>(head, that.head),
                    tailZipper.zip(that.tail));
            }
        }
    }
}
class Pair<T, S> {
    T fst; S snd;
    Pair(T f, S s) { fst = f; snd = s; }
}
class Test {
    public static void main(String[] args) {
        Seq<String> strs =
            new Seq<String>(
                "a",
                new Seq<String>("b",
                                new Seq<String>()));
        Seq<Number> nums =
            new Seq<Number>(
                new Integer(1),
                new Seq<Number>(new Double(1.5),
                                new Seq<Number>()));

        Seq<String>.Zipper<Number> zipper =
            strs.new Zipper<Number>();

        Seq<Pair<String,Number>> combined =
            zipper.zip(nums);
    }
}
```
