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. Advertising

  2. On Sep 29, 4:03 am, lyallex <> wrote:
    > However the Java 2 Platform Standard Ed. 5.0 javadoc for Method shows
    > the following signature for invoke
    >
    > Object invoke(Object obj, Object... args)


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


    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. Advertising

  3. lyallex

    Roedy Green Guest

    On Sat, 29 Sep 2007 12:03:56 +0100, lyallex <> wrote,
    quoted or indirectly quoted someone who said :

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


    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 Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 29, 2007
    #3
  4. lyallex

    lyallex Guest

    Owen Jacobson wrote:
    > On Sep 29, 4:03 am, lyallex <> wrote:
    >> However the Java 2 Platform Standard Ed. 5.0 javadoc for Method shows
    >> the following signature for invoke
    >>
    >> Object invoke(Object obj, Object... args)

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

    >
    > The second argument is a varargs argument,


    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

    Roedy Green wrote:
    > On Sat, 29 Sep 2007 12:03:56 +0100, lyallex <> wrote,
    > quoted or indirectly quoted someone who said :
    >
    >> 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.

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


    Really, I thought I'd made it quite clear, oh well, it's all sorted now.
     
    lyallex, Sep 29, 2007
    #5
  6. lyallex wrote:
    > Owen Jacobson wrote:
    >> On Sep 29, 4:03 am, lyallex <> wrote:
    >>> However the Java 2 Platform Standard Ed. 5.0 javadoc for Method
    >>> shows the following signature for invoke
    >>>
    >>> Object invoke(Object obj, Object... args)

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

    >>
    >> The second argument is a varargs argument,

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


    The ones that were introduced to make your life easier? :)
     
    Mike Schilling, Sep 29, 2007
    #6
  7. lyallex

    lyallex Guest

    Mike Schilling wrote:
    > lyallex wrote:
    >> Owen Jacobson wrote:
    >>> On Sep 29, 4:03 am, lyallex <> wrote:
    >>>> However the Java 2 Platform Standard Ed. 5.0 javadoc for Method
    >>>> shows the following signature for invoke

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

    >
    > The ones that were introduced to make your life easier? :)
    >
    >


    :) 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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JJBW
    Replies:
    1
    Views:
    10,548
    Joerg Jooss
    Apr 24, 2004
  2. n00m
    Replies:
    5
    Views:
    413
  3. Gustavo Narea
    Replies:
    14
    Views:
    896
    Gustavo Narea
    Feb 16, 2009
  4. Replies:
    7
    Views:
    205
  5. Replies:
    21
    Views:
    428
    Barry Schwarz
    Mar 5, 2014
Loading...

Share This Page