Search code examples
javaarraysjava-8variadic-functions

Legacy array notation not allowed on variable-arity parameter


I'm asking this out of curiosity as I have not found a question to this specific compile error on StackOverflow:

Legacy array notation not allowed on variable-arity parameter

I've come across legacy notation when seeing some code containing a signature like this:

private void a(int ints[]){/*...*/}

So I played around a bit until I encountered the compile error in the following samples.

// this method compiles as expected
private void method1(int[][] ints){ /*...*/ }

// this too
private void method2(int[]... ints){ /*...*/ }

// this does to, but makes use of legacy array notation
private void method3(int ints[][]){ /*...*/ }

// this still does, even when mixing up the notations
private void method4(int[] ints[]){ /*...*/ }

// this however fails to compile
private void method5(int... ints[]){ /*...*/ }

Is there a specific reason, that the language designer decided to implement it that way? To not allow varargs with legacy array notation?

NOTE: I understand the legacy notation


Solution

  • I don't know why they decided to do this*, however, I'd just like to confirm that this is a formal part of the specification and is fully intended to produce a compile-time error:

    It is a compile-time error to use mixed array notation (§10.2) for a variable arity parameter.

    From JLS 8.4.1

    *that question would be opinion-based since no one besides the Java language designers could tell you. If I were to speculate, it's likely because allowing multiple notations in the first place was a kind of bad idea.