Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 13th, 2004, 08:06 AM
Logwad Logwad is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 Logwad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Variable names for calling methods

Here is my question, is it possible to stores some names in an array of some sort and then use those names in the array to create new objects and call methods with.

Below is an example of what I am trying to do. If you have any idea of what I'm trying to do, please at least tell me the name, so I can search for tutorials.

String[] objnames = new String[2]

objnames[1] = "obj1";
objnames[2] = "obj2";

Someclass objnames[1] = new Someclass[]
Someclass objnames[2] = new Someclass[]

objnames[1].aMethodToCall
objnames[2].aMethodToCall

Reply With Quote
  #2  
Old May 26th, 2004, 08:54 PM
Logwad Logwad is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 Logwad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
So is no one even going to say that what I want to do cannot be done? I still have not been able to find anything on the subject, all I need is the name of what I am trying to do, or a tutorial reference.

Reply With Quote
  #3  
Old May 27th, 2004, 09:00 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
I don't know that you can do this in quite this manner, but you should be able to store objects in arrays and execute methods on the array elements. In languages like perl and PHP, you can do essentially what you're wanting to do by doubling up the variable marker to use the value of the variable as a name rather than a value, but I don't know that you can do this in java. I sort of doubt it.

To use your example, I think you'd want to do something more like this:

Code:

String[] objnames = new String[2];

Someclass objnames[0] = new Someclass[];
Someclass objnames[1] = new Someclass[];

objnames[0].aMethodToCall();
objnames[1].aMethodToCall();


In this case, you're just not adding the labels, which would seem to be useless to begin with, as you're using the array index to manipulate the objects anyway. If you need to store a key or label, just make it an attribute of the object and pass it in the constructor (in which case you could still have an array of labels and iterate over the array to create the objects dynamically by index, which I gather is what you're shooting for here anyway).
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #4  
Old May 27th, 2004, 10:34 AM
Logwad Logwad is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 17 Logwad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
That's just what I wanted to know, thanks!

Reply With Quote
  #5  
Old May 27th, 2004, 12:24 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
I think I screwed the syntax pretty hard, btw. Constructors would typically have parentheses rather than brackets, for example. You get the idea, though.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Variable names for calling methods


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT