Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Calling a method
Here's another thing I'm trying to do that I hope someone can help with.
I'd like to call a method on an instance of a class using a string as
the method name. Something like anInstance.perform("getName"), which
would call the method getName() on the instance and return the value
from that.

Any ideas on that?

Report this thread to moderator Post Follow-up to this message
Old Post
cdx
04-21-05 09:00 PM


Re: Calling a method
On Thu, 21 Apr 2005 12:33:43 -0400, cdx <jm1@dicehome.com> wrote:

>Here's another thing I'm trying to do that I hope someone can help with.
>  I'd like to call a method on an instance of a class using a string as
>the method name. Something like anInstance.perform("getName"), which
>would call the method getName() on the instance and return the value
>from that.

The following is the same as calling:
String testString = "Testing 123";
int result = testString.indexOf("123");


String testString = "Testing 123";
Class clazz = String.class;
Class[] parameterTypes = {String.class};

Method method = clazz.getDeclaredMethod("indexOf",
parameterTypes);
Object[] parameters = {"123"};
Object result = method.invoke(testString, parameters);

System.out.println(result.toString()); // 8

--
now with more cowbell

Report this thread to moderator Post Follow-up to this message
Old Post
Bryce
04-21-05 09:00 PM


Re: Calling a method
In article <Q8adnTn68-5TSPrfRVn-oQ@comcast.com>, jm1@dicehome.com enlightene
d
us with...
> Here's another thing I'm trying to do that I hope someone can help with.
>   I'd like to call a method on an instance of a class using a string as
> the method name. Something like anInstance.perform("getName"), which
> would call the method getName() on the instance and return the value
> from that.
>
> Any ideas on that?

You have to use reflection to do that sort of thing. There's nothing as
simple as just one line of code for it (like javascript and asp have), no.
Here are a couple options.
http://mindprod.com/jgloss/eval.html

--
--
~kaeli~
If a book about failures doesn't sell, is it a success?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace


Report this thread to moderator Post Follow-up to this message
Old Post
kaeli
04-21-05 09:00 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:28 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.