The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Re: Why does this code compile?

The place to go for an answer to questions like this is the Java Language Specification. In this case the relevant part is 15.12 Method Invocation Expressions (chapter 15 deals with the meaning of all the different sorts of expression that can arise in Java).

Be warned, the specification aims at being comprehensive and definitive not at being readily comprehensible! When it comes to this particular question, 15.12 begins with the observation that "Resolving a method name at compile time is more complicated than resolving a field name because of the possibility of method overloading. Invoking a method at run-time is also more complicated than accessing a field because of the possibility of instance method overriding". So for someone starting out the thing to understand is that the non varargs form will be preferred if there is a choice: that's just how Java is. So there is no ambiguity. And no problem with compilation.

---

The messy details are spelt out in 15.12, which shows how varargs takes its place alongside all the other things that have to be determined as the compiler attempts to compile both forms of the varargs() method, or as the runtime evaluates expressions involving that method. In particular the first step of 15.12.2 Compile-Time Step 2: Determine Method Signature says "The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase".

In other words variable arity methods are only considered for an expression like "varargs()" is there is no match with a no argument form. There is a note following this statement giving a rationale in terms of being compatible with old code (calling the method) from before Java 5, if the varargs form is added later.