Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

basics - callig methods by name

10 views
Skip to first unread message

Anjum Sabir

unread,
Mar 24, 2005, 2:34:34 PM3/24/05
to
Hi,

I am new to java and wondering how i do the following. I have a simple
program which accepts from the command line 2 numbers followed by an
operation (add, subtract, etc)

If the calculation has an associated method, how do i call the method if
i do not know it's name at runtime? All i know is that the method's name
is stored in args[3].

TIA

Anjum

--------

public static void main(String[] args) throws java.io.IOException {


String first;
String second;
String calculation;

int firstno = Integer.parseInt(args[0]);
int secondno = Integer.parseInt(args[1]);
calculation = args[3];

>> System.out.println((args[3])(firstno, secondno));



Ulf_N

unread,
Mar 24, 2005, 5:40:57 PM3/24/05
to
Anjum Sabir skrev:

You could use reflection, methods Class.getMethod() and Method.invoke().
Class 'Method' is in package java.lang.reflect. (On the other hand,
maybe it would be easier with a simple if-chain or a switch-statement.
Depends...) /ulf

0 new messages