JLS does not allow method call on Primary of array type

Lukas.P at seznam.cz Lukas.P at seznam.cz
Sat Feb 29 13:15:11 UTC 2020

Dear JLS Team,

when reading the JLS of Java 13, I think I found an omission in the section "15.12. Method Invocation Expressions". Because of that omission it is not possible to invoke methods of array type objects.

Example code:

 new int[0].clone();

When trying to find out the semantics of such a statement I go to section 15.12, as the clone identifier should identify a method to be called. That section of the specification states that determining the method to be called progresses through several steps. The section 15.12.1 gives the first step. Here, there are six cases considered.

For the given example above, I think the following case applies:

> If the form is Primary . [TypeArguments] Identifier, then let T be
> the type of the Primary expression. The class or interface to search
> is T if T is a class or interface type, or the upper bound of T if T
> is a type variable.

Now, the type of the primary expression is an array type with component type int, T is int[], T is not class or interface type, T is not a type variable. So, this case does not provide us with any class or interface to search, and the compile time determination cannot continue with any further steps.

Here, one possibility would be to consider that array type is a class type, but looking at chapter 10, we see at section 10.8:

> Although an array type is not a class, the Class object of every
> array acts as if ...

I assume this means that array types and class types should be regarded as different notions.

I think the specification should be augmented to specify where to search for a method when T is an array type.

Kind Regards,

More information about the jls-jvms-spec-comments mailing list