• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Is it possible to get variable value using a string containing variable name?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear sir,

I have a variable name = home.canvas.color.

String home.canvas.color = "purple";

If I obtain an input from user = "home.canvas.color", is there a way that I can retrieve the value "purple" from the actual variable home.canvas.color?

If so, can you show me please?

Thank you very much.

Best regards,
Yong

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kian Peng Yong wrote:
I have a variable name = home.canvas.color.
String home.canvas.color = "purple";


Is this a legal variable name in Java?
 
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

Kian Peng Yong wrote:
I have a variable name = home.canvas.color.
String home.canvas.color = "purple";


Is this a legal variable name in Java?



No, its not legal..
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have valid variable name then by using refelection API(java.lang.reflect.Field)you can retrieve the expected value.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Try this code.



The variable in your case "home.canvas.color" as to be defined as instance variable in a class. Also since java naming convention doesnt allow "." in its name you need to give some different name. Then you can follow above code to get what you are trying.

Thanks
Ashwin
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ugh. Why would you want to use reflection? If you need variables of which you do not know the names beforehand, use a Map:
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Krishnegowda wrote:
No, its not legal..


That's why, I've asked!
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ash, please UseCodeTags next time. I've replaced your asterisk lines with code tags, and not only does it not mess up the forum layout, it also preserves indentation and adds syntax highlighting.
 
Kian Peng Yong
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic