7

Consider the following (invalid) Java program:

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

    public void print(int... ints) {
        for (int i : ints) { 
            System.out.print(i);
        }
    }
}

I would expect an error similar to this:

Cannot make a static reference to the non-static method print(int[]) from the type Test
at Test.main(Test.java:5)

but instead, javac emits:

Test.java:5: error: method print in class Test cannot be applied to given types;
    print(ints);
    ^
required: int[]
found: int[]
reason: varargs mismatch; int[] cannot be converted to int
1 error

javac -version javac 1.8.0_11 java -version

java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

This came about in this question. Is this a bug in javac? Or am I missing something dead obvious here?

Community
  • 1
  • 1
jdphenix
  • 15,022
  • 3
  • 41
  • 74

1 Answers1

5

This is a bug and has been reported as JDK-8055514. (By you?)

user2864740
  • 60,010
  • 15
  • 145
  • 220
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Probably @ajb, That bug report has javac 1.8.0_05 – jdphenix Aug 20 '14 at 05:57
  • It's also a regression, `javac 1.7.0_51 output: Test.java:5: error: non-static method print(int...) cannot be referenced from a static context`, thanks for hunting down the bug reports... I knew about the ones at bugs.java.com but not here. – jdphenix Aug 20 '14 at 05:58