|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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.
|
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
That's just what I wanted to know, thanks!
|
|
#5
|
||||
|
||||
|
I think I screwed the syntax pretty hard, btw. Constructors would typically have parentheses rather than brackets, for example. You get the idea, though.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Variable names for calling methods |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|