Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / August 2009

Tip: Looking for answers? Try searching our database.

java - invoke

Thread view: 
KeeKee - 21 Aug 2009 19:53 GMT
Could anyone explain what the feature of invoke, why do we need
invoke?
Thanks in advance.
Daniel Pitts - 21 Aug 2009 19:57 GMT
> Could anyone explain what the feature of invoke, why do we need
> invoke?
> Thanks in advance.
You need to be much more specific.  As far as I know there isn't a
"feature" of Java called "invoke".  There are plenty of methods on many
different classes, and there may be VM instructions called invoke (I'm
not that knowledgeable about the VM, but I could look it up if I needed to).

So, what do you mean by the feature of invoke?
Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

KeeKee - 21 Aug 2009 20:29 GMT
On Aug 21, 2:57 pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
> > Could anyone explain what the feature of invoke, why do we need
> > invoke?
[quoted text clipped - 8 lines]
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

I see some of programs as below, and I don't quite understand. What
are the purposes of calling invoke and how to use it ? Thanks a lot.

case 1)
method = obj.getClass().getMethod(methodName, param1.class,
param2.class, ..);
method.invoke(obj, arg1, arg2,...);

case 2)
Class c = Class.forName("java.text.NumberFormat");
Method m = c.getMethod("getInstance");
Object ret = m.invoke(null);

case 3)
   Class c = Class.forName( args[0] );
   Method m = c.getMethod( args[1], new Class [] { } );
   Object ret =  m.invoke( null, null );
Lew - 21 Aug 2009 20:54 GMT
> I see some of programs as below, and I don't quite understand. What
> are the purposes of calling invoke and how to use it ? Thanks a lot.
>
> case 1)
> method = obj.getClass().getMethod(methodName, param1.class,

Here we need to know the type of the variable 'method'.  Based on the
expression assigned to it, we can safely guess that the type is
'java.lang.reflect.Method'.

> param2.class, ..);
> method.invoke(obj, arg1, arg2,...);
[quoted text clipped - 3 lines]
> Method m = c.getMethod("getInstance");
> Object ret = m.invoke(null);

Same here - we need to think of the type of the variable 'm', which is
'java.lang.reflect.Method'.

> case 3)
>     Class c = Class.forName( args[0] );
>     Method m = c.getMethod( args[1], new Class [] { } );
>     Object ret =  m.invoke( null, null );

Now that we are thinking of 'java.lang.reflect.Method', we look up the
description of its 'invoke()' method in the Javadocs to achieve
perfect understanding.
<http://java.sun.com/javase/6/docs/api/java/lang/reflect/
Method.html#invoke(java.lang.Object,%20java.lang.Object...)>

--
Lew
markspace - 22 Aug 2009 01:13 GMT
> I see some of programs as below, and I don't quite understand. What
> are the purposes of calling invoke and how to use it ? Thanks a lot.

The general feature is called reflection.

<http://java.sun.com/docs/books/tutorial/reflect/>
Roedy Green - 21 Aug 2009 23:31 GMT
On Fri, 21 Aug 2009 11:53:11 -0700 (PDT), KeeKee
<chenkeekee@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>Could anyone explain what the feature of invoke, why do we need
>invoke?
>Thanks in advance.

You have stumbled on one of the most subtle features of Java called
reflection.  Basically it allows you to call methods at run time that
were not written at compile time, and further whose structure too was
unknown, e.g. the names of the methods and the types of the
parameters.

You create call lists of parameters on the fly and invoke methods.
You can probe to find out what methods are available and what parms
they want.

see http://mindprod.com/jgloss/reflection.html

It is a tool to allow java code to use arbitrary class files handed to
it at run time.

You can usually get what you want without resorting to reflection
using dynamic loading.
see http://mindprod.com/jgloss/classforname.html
Signature

Roedy Green Canadian Mind Products
http://mindprod.com

http://thecovemovie.com : The Cove: a documentary about Japan's secret atrocities against dolphins.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.