Jan 232007
 

I may have just hit a nasty JDK varargs issue. If you have two constructors with the same number of non-vararg parameters and one of them has an additional varargs parameter, you need to be very careful which is called. Here is the code:

It looks like Java favors matching on parameter numbers and not types. If you invoke this method like this:

It invokes the SECOND version and NOT the first! Looks like the JDK matches on the number of parameters (2) and ignores the fact that the first version of the method matches better because of typing and the fact that varargs is empty.

UPDATE;
Thanks to a commenter, I found the section in the JLS that determines if variable argument methods are invoked:

“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.”

This states that in order to keep backwards compatibility, if first ignored methods with variable arguments (variable arity). This is ONLY for backwards compatibility and if JDK 5.0 was really a major release with no compatibility than my initial assessment of this being a bug would be true.

  One Response to “Nasty JDK varargs bug?”

  1. Is there a difference between arguments (String foo, String… bar) and (String… foobar)? I’m not sure and the guidelines say to be careful with it anyway:

    jdk 5 guide

    Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called.

    Here’s the definitive statement on it, probably only requiring a couple of weeks of reading to figure out:

    JLS 3rd edition

    let’s hope this comes through without a preview

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">