---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 26 Edition"
java_version: 26
chapter: "Chapter 4. Types, Values, and Variables"
section: "4.11"
heading: "4.11. Where Types Are Used"
parent: "Chapter 4. Types, Values, and Variables"
source: https://docs.oracle.com/javase/specs/jls/se26/html/jls-4.html#jls-4.11
id: "4.11"
url: https://jvm.obfuscated.software/jls/se26/4.11.html
raw: https://jvm.obfuscated.software/jls/se26/4.11.md
---

# 4.11. Where Types Are Used

Types are used in most kinds of declaration and in certain kinds of expression. Specifically, there are 17 *type contexts* where types are used:

- In declarations:

  1. A type in the `extends` or `implements` clause of a class declaration ([§8.1.4](8.1.4.md), [§8.1.5](8.1.5.md))
  2. A type in the `extends` clause of an interface declaration ([§9.1.3](9.1.3.md))
  3. The return type of a method ([§8.4.5](8.4.5.md), [§9.4](9.4.md)), including the type of an element of an annotation interface ([§9.6.1](9.6.1.md))
  4. A type in the `throws` clause of a method or constructor ([§8.4.6](8.4.6.md), [§8.8.5](8.8.5.md), [§9.4](9.4.md))
  5. A type in the `extends` clause of a type parameter declaration of a generic class, interface, method, or constructor ([§8.1.2](8.1.2.md), [§9.1.2](9.1.2.md), [§8.4.4](8.4.4.md), [§8.8.4](8.8.4.md))
  6. The type in a field declaration of a class or interface ([§8.3](8.3.md), [§9.3](9.3.md)), including an enum constant ([§8.9.1](8.9.1.md))
  7. The type in a formal parameter declaration of a method, constructor, or lambda expression ([§8.4.1](8.4.1.md), [§8.8.1](8.8.1.md), [§9.4](9.4.md), [§15.27.1](15.27.1.md))
  8. The type of the receiver parameter of a method ([§8.4](8.4.md))
  9. The type in a local variable declaration in either a statement ([§14.4.2](14.4.2.md), [§14.14.1](14.14.1.md), [§14.14.2](14.14.2.md), [§14.20.3](14.20.3.md)) or a pattern ([§14.30.1](14.30.1.md))
  10. A type in an exception parameter declaration ([§14.20](14.20.md))
  11. The type in a record component declaration of a record class ([§8.10.1](8.10.1.md))

- In expressions:

  12. A type in the explicit type argument list to a constructor invocation, class instance creation expression, method invocation expression, or method reference expression ([§8.8.7.1](8.8.7.1.md), [§15.9](15.9.md), [§15.12](15.12.md), [§15.13](15.13.md))
  13. In an unqualified class instance creation expression, as the class type to be instantiated ([§15.9](15.9.md)) or as the direct superclass type or direct superinterface type of an anonymous class to be instantiated ([§15.9.5](15.9.5.md))
  14. The element type in an array creation expression ([§15.10.1](15.10.1.md))
  15. The type in the cast operator of a cast expression ([§15.16](15.16.md))
  16. The type that follows the `instanceof` type comparison operator ([§15.20.2](15.20.2.md))
  17. In a method reference expression ([§15.13](15.13.md)), as the reference type to search for a member method or as the class type or array type to construct.

Also, types are used as:

- The element type of an array type in any of the above contexts; and
- A non-wildcard type argument, or a bound of a wildcard type argument, of a parameterized type in any of the above contexts.

Finally, there are three special terms in the Java programming language which denote the use of a type:

- An unbounded wildcard ([§4.5.1](4.5.1.md))
- The `...` in the type of a variable arity parameter ([§8.4.1](8.4.1.md)), to indicate an array type
- The simple name of a type in a constructor declaration ([§8.8](8.8.md)), to indicate the class of the constructed object

The meaning of types in type contexts is given by:

- [§4.2](4.2.md), for primitive types
- [§4.4](4.4.md), for type parameters
- [§4.5](4.5.md), for class and interface types that are parameterized, or appear either as type arguments in a parameterized type or as bounds of wildcard type arguments in a parameterized type
- [§4.8](4.8.md), for class and interface types that are raw
- [§4.9](4.9.md), for intersection types in the bounds of type parameters
- [§6.5](6.5.md), for types of non-generic classes, interfaces, and type variables
- [§10.1](10.1.md), for array types

Some type contexts restrict how a reference type may be parameterized:

- The following type contexts require that if a type is a parameterized reference type, it has no wildcard type arguments:

  - In an `extends` or `implements` clause of a class declaration ([§8.1.4](8.1.4.md), [§8.1.5](8.1.5.md))
  - In an `extends` clause of an interface declaration ([§9.1.3](9.1.3.md))
  - In an unqualified class instance creation expression, as the class type to be instantiated ([§15.9](15.9.md)) or as the direct superclass type or direct superinterface type of an anonymous class to be instantiated ([§15.9.5](15.9.5.md))
  - In a method reference expression ([§15.13](15.13.md)), as the reference type to search for a member method or as the class type or array type to construct.

  In addition, no wildcard type arguments are permitted in the explicit type argument list to a constructor invocation or class instance creation expression or method invocation expression or method reference expression ([§8.8.7.1](8.8.7.1.md), [§15.9](15.9.md), [§15.12](15.12.md), [§15.13](15.13.md)).

- The following type contexts require that if a type is a parameterized reference type, it has only unbounded wildcard type arguments (i.e. it is a reifiable type) :

  - As the element type in an array creation expression ([§15.10.1](15.10.1.md))
  - As the type that follows the `instanceof` relational operator ([§15.20.2](15.20.2.md))

- The following type contexts disallow a parameterized reference type altogether, because they involve exceptions and the type of an exception is non-generic ([§6.1](6.1.md)):

  - As the type of an exception that can be thrown by a method or constructor ([§8.4.6](8.4.6.md), [§8.8.5](8.8.5.md), [§9.4](9.4.md))
  - In an exception parameter declaration ([§14.20](14.20.md))

In any type context where a type is used, it is possible to annotate the keyword denoting a primitive type or the *Identifier* denoting the simple name of a reference type. It is also possible to annotate an array type by writing an annotation to the left of the `[` at the desired level of nesting in the array type. Annotations in these locations are called *type annotations*, and are specified in [§9.7.4](9.7.4.md). Here are some examples:

- `@Foo int[] f;` annotates the primitive type `int`
- `int @Foo [] f;` annotates the array type `int[]`
- `int @Foo [][] f;` annotates the array type `int[][]`
- `int[] @Foo [] f;` annotates the array type `int[]` which is the component type of the array type `int[][]`

Some of the *type contexts* which appear in declarations occupy the same syntactic real estate as a number of *declaration contexts* ([§9.6.4.1](9.6.4.1.md)):

- The return type of a method (including the type of an element of an annotation interface)
- The type in a field declaration of a class or interface (including an enum constant)
- The type in a formal parameter declaration of a method, constructor, or lambda expression
- The type in a local variable declaration
- The type in an exception parameter declaration
- The type in a record component declaration of a record class

The fact that the same syntactic location in a program can be both a type context and a declaration context arises because the modifiers for a declaration immediately precede the type of the declared entity. [§9.7.4](9.7.4.md) explains how an annotation in such a location is deemed to appear in a type context or a declaration context or both.

**Example 4.11-1. Usage of a Type**

```
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;

class MiscMath<T extends Number> {
    int divisor;
    MiscMath(int divisor) { this.divisor = divisor; }
    float ratio(long l) {
        try {
            l /= divisor;
        } catch (Exception e) {
            if (e instanceof ArithmeticException)
                l = Long.MAX_VALUE;
            else
                l = 0;
        }
        return (float)l;
    }
    double gausser() {
        Random r = new Random();
        double[] val = new double[2];
        val[0] = r.nextGaussian();
        val[1] = r.nextGaussian();
        return (val[0] + val[1]) / 2;
    }
    Collection<Number> fromArray(Number[] na) {
        Collection<Number> cn = new ArrayList<Number>();
        for (Number n : na) cn.add(n);
        return cn;
    }
    <S> void loop(S s) { this.<S>loop(s); }
}
```

In this example, types are used in declarations of the following:

- Fields, which are the class variables and instance variables of classes ([§8.3](8.3.md)), and constants of interfaces ([§9.3](9.3.md)); here the field `divisor` in the class `MiscMath` is declared to be of type `int`
- Method parameters ([§8.4.1](8.4.1.md)); here the parameter `l` of the method `ratio` is declared to be of type `long`
- Method results ([§8.4](8.4.md)); here the result of the method `ratio` is declared to be of type `float`, and the result of the method `gausser` is declared to be of type `double`
- Constructor parameters ([§8.8.1](8.8.1.md)); here the parameter of the constructor for `MiscMath` is declared to be of type `int`
- Local variables ([§14.4](14.4.md), [§14.14](14.14.md)); the local variables `r` and `val` of the method `gausser` are declared to be of types `Random` and `double[]` (array of `double`)
- Exception parameters ([§14.20](14.20.md)); here the exception parameter `e` of the `catch` clause is declared to be of type `Exception`
- Type parameters ([§4.4](4.4.md)); here the type parameter of `MiscMath` is a type variable `T` with the type `Number` as its declared bound
- In any declaration that uses a parameterized type; here the type `Number` is used as a type argument ([§4.5.1](4.5.1.md)) in the parameterized type `Collection<Number>`.

and in expressions of the following kinds:

- Class instance creations ([§15.9](15.9.md)); here a local variable `r` of method `gausser` is initialized by a class instance creation expression that uses the type `Random`
- Generic class ([§8.1.2](8.1.2.md)) instance creations ([§15.9](15.9.md)); here `Number` is used as a type argument in the expression `new ArrayList<Number>()`
- Array creations ([§15.10.1](15.10.1.md)); here the local variable `val` of method `gausser` is initialized by an array creation expression that creates an array of `double` with size 2
- Generic method ([§8.4.4](8.4.4.md)) or constructor ([§8.8.4](8.8.4.md)) invocations ([§15.12](15.12.md)); here the method `loop` calls itself with an explicit type argument `S`
- Casts ([§15.16](15.16.md)); here the `return` statement of the method `ratio` uses the `float` type in a cast
- The `instanceof` operator ([§15.20.2](15.20.2.md)); here the `instanceof` operator tests whether `e` is assignment-compatible with the type `ArithmeticException`
