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

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

  1. lyallex

    lyallex Guest

    Hi

    This has got me stumped

    jdk1.5.0_10

    I can compile and run the code for RunTests and associated classes at
    http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html

    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 {
    m.invoke(null);
    ....

    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

    Duncan
     
    lyallex, Sep 29, 2007
    #1
    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
     
    Owen Jacobson, Sep 29, 2007
    #2
    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
    #3
  4. lyallex

    lyallex Guest

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

    Thanks for that

    Duncan
     
    lyallex, Sep 29, 2007
    #4
  5. lyallex

    lyallex Guest

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