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..
SCJP 5 (93%)
subodh k kumar
Joined: Aug 19, 2009
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
Joined: Jun 14, 2006
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.
Joined: Oct 14, 2008
Ugh. Why would you want to use reflection? If you need variables of which you do not know the names beforehand, use a Map: