• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Dynamic variable names

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my situation:

I am maintaining an old program and I have a situation where I have a group of objects named item1, item2, item3, etc. with a .doStuff() method.

Is there a way to build a call to those items so I can do something like this:



I think this is possible but I have no clue how.

Thanks, as always!
 
Marshal
Posts: 27366
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Replacing them with an array of the same type would be the simplest thing to do.
 
Daniel Gallant
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I thought.

Thank you for your help!

On a side note, is this even doable?
 
Paul Clapham
Marshal
Posts: 27366
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Gallant wrote:On a side note, is this even doable?



Based on the code I have seen, I would have to say "Maybe".
 
lowercase baba
Posts: 13069
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO, there is no need to do so. Variable names are for programmers, not the JVM.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use Java reflection (java.lang.reflect.*) to obtain the names of all the variables, and refer to them by name. I'm not sure why you would want to, but that's the java feature that enables that kind of manipulation.

I would be more interested in Eclipse's feature to change all the uses of one name of a variable to something else, throughout a project.

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dynamic varaibles are used in some interpreted languages like tcl where all the variables are treated as type independent.

set a 1
set b 2
set c some$a
puts $c
set d [expr $a + $ b]
puts $d

Ouput will be
some1
3

But as java is compiled i think the reflection is the way to go.Refer https://coderanch.com/t/324748/java/java/Dynamic-Variable-naming-Java-at
 
Yup, yup, yup. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic