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

L

lyallex

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
 
O

Owen Jacobson

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
 
R

Roedy Green

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

lyallex

Roedy said:
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.
 
L

lyallex

Mike said:
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 ;-)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top