Method.invoke() with one arg compiles despite method signature

Discussion in 'Java' started by lyallex, Sep 29, 2007.

  1. lyallex

    lyallex Guest


    This has got me stumped


    I can compile and run the code for RunTests and associated classes at

    However the Java 2 Platform Standard Ed. 5.0 javadoc for Method shows
    the following signature for invoke

    Object invoke(Object obj, Object... args)

    The particular bit of the code that is causing me a problem is

    try {

    I have looked at the code for Method and there is only one 2 arg method
    called invoke. How then am I managing to compile and run the
    aforementioned code ... surely the compiler should flag this as a
    compile time error. Obviously I'm missing something but I can't see what.

    What am I missing ? ...

    Thanks in advance

    lyallex, Sep 29, 2007
    1. Advertisements

  2. The second argument is a varargs argument, which is translated to an
    array automatically by the compiler. If omitted, the varargs argument
    is populated with an empty array.

    Owen Jacobson, Sep 29, 2007
    1. Advertisements

  3. lyallex

    Roedy Green Guest

    I had to read your post several time to figure out why you thought
    that code should not work.

    Object ... arg --- the parm list can also be empty (unlike the ...
    ellipsis in English). This effectively gives you a 1-arg constructor.
    Roedy Green, Sep 29, 2007
  4. lyallex

    lyallex Guest

    Oh ******, and I thought I had got a handle on all the new 1.5 features.

    Thanks for that

    lyallex, Sep 29, 2007
  5. lyallex

    lyallex Guest

    Really, I thought I'd made it quite clear, oh well, it's all sorted now.
    lyallex, Sep 29, 2007
  6. The ones that were introduced to make your life easier? :)
    Mike Schilling, Sep 29, 2007
  7. lyallex

    lyallex Guest

    :) Yea, they're the ones. Sometimes I think that some of these
    'enhancements' are only invented to give anal interviewers something to
    pick up on to make themselves feel superior when they are interviewing
    you for some squalid little job in some ghastly sweatshop.

    Never give up, never surrender.

    Galaxy Quest, the best film EVER ;-)
    lyallex, Sep 29, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.