This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to build a String then use it as a variable name?

 
Garann Means
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to avoid a ridiculous number of 'if's, and the only way I can think of to do so is to build the variable names of constants that are defined elsewhere in the API. Can anyone help?
Thanks,
g.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand your question exactly.
Could you elaborate on what you mean?
 
Garann Means
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually got it figured out. I did this:

Where DORF0324 is a static class.
Thanks anyway!
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
huh??? (scratching my head)
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and you're using reflection just to reduce the amout of code you have to write
Um, have you tried profiling the amount of time to do each? In JDK1.2, method invokation using reflection took 200x longer than calling the method directly. And thats when you cache methods rather than looking them up each time.
It apparently got better in JDK1.3 and is now more like 20x as long, but is this really what you want when a second solution might be to format the code differently?
 
Karl Laird
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We might be able to help more if you post an example of the code you are trying to streamline.
Without, it is difficult to make intellegent suggestions - would a case statement be appropriate?
static final constants can often be used to provide mappings to more complex combinations circumventing the limitation of int / char cases.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic