473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

reference methods or fields by "name"

Is there any way of referencing a method or field dynamically by a name
contained in a string?

thanks
D
Jul 17 '05 #1
2 1513
You can use the java.land.reflect API to do this.

example:
Class clazz = Class.forName("xyz");
Object obj = clazz.newInstance();
Method meth = clazz.getMethod("toString", null);
Object ret = meth.invoke(obj, null);

danjourno wrote:
Is there any way of referencing a method or field dynamically by a name
contained in a string?

thanks
D

Jul 17 '05 #2
On Mon, 27 Sep 2004 00:01:11 +1000, danjourno wrote:
Is there any way of referencing a method or field dynamically by a name
contained in a string?

thanks
D


Method m = obj.getClass().getMethod(methodname, Object[] parameters)
returns the method you want to call on obj. To invoke it:
m.invoke(obj, Object[] parameters)

You can see the Method class in java.lang.reflect

Tony
Jul 17 '05 #3

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

Similar topics

3
by: Pavils Jurjans | last post by:
Hello, I have bumped upon this problem: I do some client-side form processing with JavaScript, and for this I loop over all the forms in the document. In order to identify them, I read their...
16
by: Gufus | last post by:
Hi Group... <a name="#top"></a> ----del---- ----del---- <img SRC="images/top.gif" USEMAP="#top" BORDER=0> <map NAME="top">
2
by: Murphy | last post by:
Our website contains subdirectories for each subsidiary company, each company has it's own look and feel to the pages in their subdirectory although they are all part of the main website. The...
6
by: Cecil | last post by:
It's blue in SQL Studio but I can't find it in SQL Books on-line. I wanted to use it as a field name but don't like using . Is it reserved?
13
by: dhughey | last post by:
I am using <script type="text/javascript"> function setAction(frm){ act = ''; for(x=0;x<frm.se.length;x++){ if(frm.se.checked){ act = frm.se.value; } }
1
by: alain MONTMORY | last post by:
Hello everybody, I am a newbie to python so I hope I am at the right place to expose my problem..... :-http://www.python.org/doc/2.4.2/ext/pure-embedding.html 5.3 Pure Embedding I download the...
1
by: Terry Olsen | last post by:
I have written a class library that I want to reference from a windows app. The filename is UPSTrackTool.dll. The Class name is UPSTrackTool. When I reference it from my windows app, I put the...
1
by: mark | last post by:
Forgive me if this seems like a stupid question but I need help... I'm trying to do a simple online form that emails me the results from a few fields. Here is the code: <form...
4
by: KrazyKasper | last post by:
I'm a Novice User using Access 2003 Tables are via ODBC (i.e., cannot alter fields) I have a report that uses the field OrderRepName (text string) and is formatted as lastname, firstname,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.