wood burning stoves 2.0*
The moose likes Java in General and the fly likes Is it possible to get variable value using a string containing variable name? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is it possible to get variable value using a string containing variable name?" Watch "Is it possible to get variable value using a string containing variable name?" New topic
Author

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

Kian Peng Yong
Greenhorn

Joined: Aug 29, 2009
Posts: 7
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

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

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..


Regards, Prasad
SCJP 5 (93%)
subodh k kumar
Greenhorn

Joined: Aug 19, 2009
Posts: 16
If you have valid variable name then by using refelection API(java.lang.reflect.Field)you can retrieve the expected value.


SCJP1.4 , SCWCD 5
Ash Kondhalkar
Ranch Hand

Joined: Jun 14, 2006
Posts: 43
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
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Nov 04, 2009
Posts: 2066

Prasad Krishnegowda wrote:
No, its not legal..

That's why, I've asked!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kian Peng Yong
Greenhorn

Joined: Aug 29, 2009
Posts: 7
Thank you very much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to get variable value using a string containing variable name?
 
Similar Threads
PL/SQL: getting table counts for multiple tables
"this" is optional ?
Xpath - more doubts....
Variable or An Object
Can we modify a private variable outside a class?