473,473 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

basics - callig methods by name

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));


Jul 18 '05 #1
1 1902
Anjum Sabir skrev:
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));



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
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
1
by: Dave Merrill | last post by:
Python newb here. Say a class contains some rich attributes, each defined as a class. If an instance of the parent class recieves a call to a method belonging to one of those attributes, it should...
18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
22
by: Luke | last post by:
Elements with name attribute: form, input, textarea, a, frame, iframe, button, select, map, meta, applet, object, param, img (if you know more reply...) Methods of addresing html elements:...
8
by: Jason | last post by:
A week ago I posted a simple little hi-score routine that I was using to learn Python. I've only just managed to examine the code, and the responses that people gave, and I'm now seriously...
5
by: bayerj | last post by:
I want to make a registry of methods of a class during creation. My attempt was this """ classdecorators.py Author: Justin Bayer Creation Date: 2006-06-22 Copyright (c) 2006 Chess Pattern...
3
by: Phillip Conrad | last post by:
Here is a little problem I've run into, and none of the naming conventions have helped... Ever since I switched from C to C# and FxCop, I've going crazy trying to fix some style issues. I have 3...
3
by: R69D | last post by:
Hi, I'm real new to Java and need some help...was going through some old tutorials and need help with these questions: 1)What is “composition of methods?” How do u define the composition of...
0
by: Sharath | last post by:
Quality Globe is Glad to Offer you the Fast Track course on Automation, QTP Basics and Advanced, and Quality Center Starting Date: June 4th, 2007 Timings: 10 AM to 3:30 PM Duration: 50 Hours ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.