Search code examples
javalanguage-features

Java language spec: "an invocation of Class" as return type of method in annotation type


The Java 8 Language Spec says in the discussion of annotation type elements :

The return type of a method declared in an annotation type must be one of the following, or a compile-time error occurs:

... Class or an invocation of Class (§4.5)

What is meant with "an invocation of Class"? Just something like Class<Number> , or more than that?


Solution

  • Yes, that's exactly that. The terminology is confusing, rarely used, and I can't seem to find it anywhere in the JSL. It is in a tutorial, though:

    To reference the generic Box class from within your code, you must perform a generic type invocation, which replaces T with some concrete value, such as Integer:

    Box<Integer> integerBox;
    

    You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argument — Integer in this case — to the Box class itself.

    Type Parameter and Type Argument Terminology: Many developers use the terms "type parameter" and "type argument" interchangeably, but these terms are not the same. When coding, one provides type arguments in order to create a parameterized type. Therefore, the T in Foo<T> is a type parameter and the String in Foo<String> f is a type argument. This lesson observes this definition when using these terms.

    Like any other variable declaration, this code does not actually create a new Box object. It simply declares that integerBox will hold a reference to a "Box of Integer", which is how Box<Integer> is read.

    An invocation of a generic type is generally known as a parameterized type.