> Thanks, I think this question can be solved by reflecting. but I want
> to more easy way. maybe like some method can transfer a variable name
[quoted text clipped - 12 lines]
>
> Thanks.
Well, Because I need some new variable, ex. SESSION_USER_NAME or
something. and maybe I will use this variable as a session name or a
key of hashmap , or in web application I need a event base on a
field....
////////////////////////
class A {
String userName;
}
...
// I want to create a new session to storge userName
String SESSION_USER_NAME = "session_user_name"; // I want to get
"session_user_name" automatically.
// or I want to create a new key for a hashmap....
...
///////////////////////
in this case, I really don't like name the variable value manually, at
least when I refactor the "userName", I don't need to change variable
value by myself.
> > Thanks, I think this question can be solved by reflecting. but I want
> > to more easy way. maybe like some method can transfer a variable name
[quoted text clipped - 17 lines]
>
> Patricia
Luc The Perverse - 30 Jun 2006 04:20 GMT
> Well, Because I need some new variable, ex. SESSION_USER_NAME or
> something. and maybe I will use this variable as a session name or a
> key of hashmap , or in web application I need a event base on a
> field....
Aha!
The mystery is out.
I have done things like this many times. (In my systems it made more sense
to refer to fields by number and then use an enumeration if necessary in the
program, but if you do indeed need a string reference, then I would suggest
making a class something like this:
class RefData{
String objectName;
String data;
}
You can then easily put it into an array, make lookups etc. And your
referencing ability will not be limited to names that you assigned in your
program.
You can implement a comparable class and have them sorted by objectName (or
hashed if you desire)
--
LTP
:)