3

Why won't this compile?

public class PrimitiveVarArgs
{
    public static void main(String[] args)
    {
        int[] ints = new int[]{1, 2, 3, 4, 5};
        prints(ints);
    }

    void prints(int... ints)
    {
        for(int i : ints)
            System.out.println(i);
    }
}

It complains about line 5, saying:

method prints in class PrimitiveVarArgs cannot be applied to given types;
  required: int[]
  found: int[]
  reason: varargs mismatch; int[] cannot be converted to int

but as far as I (and others on SO) know, int... is the same as int[]. This works if it's a non-primitive type, like String, but not on primitives.

I can't even add this method:

void prints(int[] ints)
{
    for(int i : ints)
        System.out.println(i);
}

because the compiler says:

name clash: prints(int[]) and prints(int...) have the same erasure

cannot declare both prints(int[]) and prints(int...) in PrimitiveVarArgs

so, why doesn't Java let you pass a native array to a varargs method? Also, if you would please, offer me a way to solve this issue (i.e. provide a way to pass variable arguments or an array to this method).

Community
  • 1
  • 1
Ky -
  • 30,724
  • 51
  • 192
  • 308

1 Answers1

10

Fix this in your code and it'll work:

static void prints(int... ints) // notice the static keyword at the beginning!

The problem is not with the varargs, it's with the way you're calling an instance method from a static context. Also, make extra-sure that there are no other methods with conflicting signatures, for example these two methods will look the same to the compiler:

void prints(int... ints)
void prints(int[]  ints)
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 6
    The error message `javac` produces on the OP's code is weird enough that I'm considering reporting it as a bug, although I'm sure that it's a low priority because the compiler is essentially behaving correctly (i.e. not compiling it). – ajb Aug 20 '14 at 00:34
  • 2
    Strangely the java 6 jdk gives me a better error than the java 8 jdk: `Error:(6, 9) java: PrimitiveVarArgs.java:6: non-static method prints(int...) cannot be referenced from a static context` – Volune Aug 20 '14 at 00:36
  • @Supuhstar there's two issues here, the first error you were getting was emitted by javac (and one that doesn't fit the cause at that...) – jdphenix Aug 20 '14 at 00:53
  • @Supuhstar it looks like a compiler- or IDE-specific problem. In my machine the warning doesn't show up! maybe you should turn off that compiler's warning in the configuration, or use an annotation to hide it. Or just plain ignore it :P in Java there are unsolvable warnings, specially when dealing with types – Óscar López Aug 20 '14 at 00:55
  • 1
    That's why I changed the question to be IDE-specific – Ky - Aug 20 '14 at 01:04
  • 2
    @ÓscarLópez The OP edited their question to be completely different. – jdphenix Aug 20 '14 at 02:02
  • @jdphenix kind of, but the core question is still the same (why is it complaining about passing an array to varargs); the goal is just no longer making it compile anywhere, but getting rid of the NetBeans-specific warning – Ky - Aug 20 '14 at 02:53
  • @Supuhstar if the warning is indeed a bug (as stated in @jdphenix's answer) then you won't be able to get rid of it, until the bug is fixed. Why don't you use `@SuppressWarnings` for the time being? I know it's annoying to have a warning, but as I said before, sometimes in Java there is no way to avoid them. – Óscar López Aug 20 '14 at 02:57
  • 1
    @Supuhstar The original error is a probable bug in javac (see @ajb), although still correctly not compiling an invalid program, while the new error is a bug in NetBeans. – jdphenix Aug 20 '14 at 03:06
  • 5
    @Supuhstar It's a new error message, referring to the Netbeans precompiler instead of javac, and it has a different cause (guessing between this answer and jdphenix's). You should have asked a new question and not edited this one. – Chris Hayes Aug 20 '14 at 03:28
  • Jeez, fine, I reverted my edits. – Ky - Aug 20 '14 at 15:46