reflection question

D

Daniel Wang

How do I invoke a method on a class that does not have any parameters and
returns void?

For example, how to invoke this method?

public void printHello()
{
System.out.println( "Hello!" );
}


Thanks in advance.
 
S

sks

Daniel Wang said:
How do I invoke a method on a class that does not have any parameters and
returns void?

For example, how to invoke this method?

public void printHello()
{
System.out.println( "Hello!" );
}

You get your method with getMethod or getDeclaredMethod if its private /
package / protected and pass in null for the Class parameter types when its
a method with zero args. Invoke it with null again for the object
parameters. The method will return null if its a void.

It tells you all this in the invoke method javadoc so you looked hard didn't
you.
 
T

Tony Morris

sks said:
You get your method with getMethod or getDeclaredMethod if its private /
package / protected and pass in null for the Class parameter types when its
a method with zero args. Invoke it with null again for the object
parameters. The method will return null if its a void.

It tells you all this in the invoke method javadoc so you looked hard didn't
you.

You do not pass null.
You pass nothing to the varargs parameter.

e.g. m.invoke(theObject);

It tells you all this in the invoke method javadoc so you looked hard didn't
you.
 
S

sks

Tony Morris said:
You do not pass null.
You pass nothing to the varargs parameter.

e.g. m.invoke(theObject);

Only in 1.5. Before that you do m.invoke(obj, null) or m.invoke(obj, new
Object[] {});

And as that's more compatible its best to do it that way.
 
T

Tony Morris

sks said:
Tony Morris said:
parameters
and when
its

You do not pass null.
You pass nothing to the varargs parameter.

e.g. m.invoke(theObject);

Only in 1.5. Before that you do m.invoke(obj, null) or m.invoke(obj, new
Object[] {});

And as that's more compatible its best to do it that way.

It is not more "compatible" and it is not "best".

Prior to 1.5, the typical way of invoking a noargs method through reflection
was by passing null as the second argument.
Prior to motor vehicles, we used horses.

Today, we invoke a method through reflection by passing nothing to the
varargs parameters (converted to a zero-length String array).
Today, we drive cars.
 
S

sks

Only in 1.5. Before that you do m.invoke(obj, null) or m.invoke(obj, new
Object[] {});

And as that's more compatible its best to do it that way.

It is not more "compatible" and it is not "best".

How is it not more compatible? Doing it your way works on 1.5 only, doing it
the other way works on previous versions too. That is quite obviously more
compatible.
Prior to 1.5, the typical way of invoking a noargs method through reflection
was by passing null as the second argument.
Prior to motor vehicles, we used horses.

Today, we invoke a method through reflection by passing nothing to the
varargs parameters (converted to a zero-length String array).
Today, we drive cars.

Patronising comment from a dickhead apart, I quite agree with you that
people should move on, I can't stand all this backward compatibility back to
version 1.0 rubbish, but what I said is still correct.
 
F

Filip Larsen

Tony Morris wrote
Prior to 1.5, the typical way of invoking a noargs method through reflection
was by passing null as the second argument.
Prior to motor vehicles, we used horses.

Using that parallel, you may consider us back in 1900 with most people
still traveling by horse. Hopefully 1.5 is the future, but until then it
make much sense to give advice that work on 1.4 or at least note when
its 1.5 only.

If you had wanted to give the impression that you knew the difference
between 1.4 and 1.5 in the context of the advice "sks" gave, then all
you had to do was to say something like "and in 1.5 you can do it like
this...". Not that I think you care, but to me your patronizing reply
about him not knowing the API only seem to reflect back on you.


Regards,
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top