How to transfer/get a attribute name as a String?

J

Joey

Hello everybody.

I got a problem, I want to some thing like this.

---- String age;

I want to get the its name "age".

Thanks.

Joey
 
I

IchBin

Joey said:
Hello everybody.

I got a problem, I want to some thing like this.

---- String age;

I want to get the its name "age".

Thanks.

Joey

You could use java.lang.reflect to get it.

http://javaalmanac.com/egs/java.lang.reflect/Fields.html

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/Field.html


Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
J

Joey

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
as a String...
because what my need is just get only one field name sometime. if I use
reflecting , I have to write a loop to compare each field in a class(I
think).

my real code maybe like this.

String id = "abc";
String SESSION = "SESSION_";
String SESSION_ID = SESSION + (id' s name);
// here I need to create a SESSION_ID , value should be SESSION_id, but
I don't want to //hardcode the id's name.

Thanks.
 
P

Patricia Shanahan

Joey said:
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
as a String...
because what my need is just get only one field name sometime. if I use
reflecting , I have to write a loop to compare each field in a class(I
think).

my real code maybe like this.

String id = "abc";
String SESSION = "SESSION_";
String SESSION_ID = SESSION + (id' s name);
// here I need to create a SESSION_ID , value should be SESSION_id, but
I don't want to //hardcode the id's name.

Thanks.

Could you perhaps back up a level, and explain why you want to use part
of a field name, rather than a literal or a value from a Map etc.?

Patricia
 
J

Joey

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

Luc The Perverse

Joey said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top