• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamic variable names

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
Can you dynamically create a variable name in Java? I have an application that parses through XML data and gets an unknown number of a data object type. I want to be able to create a new data object with a new variable name corresponding to its ID everytime I get one. Is this do-able? I know I could do it with ejbeans, but don't want to use ejbeans in this case.
Thanks for the help!
Regards,
Omar....
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't create a variable name dynamically in Java. To do what you're trying to do, you could create a HashMap to store all the variables pertaining to a given record / object / file / whatever. If the XML parser tells you there is an element named "author" with a value of "John Steinbeck", use
<code><pre>map.put("author", "John Steinbeck")</pre></code>
to store the value. Then later when you need to access it, use
<code><pre>String value = map.get("author")</pre></code>
Will that work for you?
 
reply
    Bookmark Topic Watch Topic
  • New Topic