• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating an object using a variable name?  RSS feed

 
Jamie Wool
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to create an object using a variable as it's name, e.g.



where shrubid is an interger variable. Basically i have a unique integer variable and thought i could use that to name my object, so i could then say 1002.getName(). Is it possible, what r my options.
 
Ulas Ergin
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have any idea why you should need this?

Anyway shrudid can not be an integer value since variable names in java can not start with a digit.

If you know that first x chars are nondigit and the rest is digit you can substring starting from the first x chars.

there exists a method getName() for java.lang.reflect.Fields which represents a member field of a class, check if that works for you
 
Jamie Wool
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically i was attempting to build a set of objects from this xml file



I need to find a neat way of accessing the objects, so as the shrubid is unique i thought that this would be useful. Basically if i could use the shrubid as part of the object addressing then it means i can access the object from anywhere instantly with the shrubid without having to look through any control structure.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a Class with getter & setter methods for each "shrub"
You can use any collection classes to store these "shrub" class where shrub.Id forms the key in the collection.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, i dont think variable/method names can be dynamic when you declrae them. Ofcourse you could use reflection to
invoke a method given its name (as a String).

Lets assume, for a second that what you seek is possible.
So you would have two shrub objects each referred by 00220 and 00001. Tell me, how would you establish that
the object referred to by 00220 refers to cactus and that referred to by 00001 refers to sunflower.
You would have to store this info somewhere right ?
Or else you will have to check for each object. The only ease you would have would be that rather than iterating over a collection of objects, you would know the reference names. But what does that achieve ?

Why then, dont you store the objects in some collection and query each to get to the object you desire?
You would be still doing all that you would have done if you had the functionality you seek.

Are you able to understand that Iam trying to say ?

ram.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to model these specific XML elements, you can create a Shrub class that holds each attribute in a separate field, as Srinivasa said. Using getter and setter methods (accessors) will make it conform to the JavaBean specification, if you want something to look up on Google or java.sun.com.

If you have many XML files with different types of objects, there are tools like JAXB that will generate the Beans and mapping classes to read in and parse the XML into objects.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!