Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Joey - 28 Jun 2006 09:18 GMT
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
Jean-Francois Briere - 28 Jun 2006 09:51 GMT
You have to use the reflection API to get the class member names.
Here is info on it:
http://java.sun.com/docs/books/tutorial/reflect/index.html

Regards
IchBin - 28 Jun 2006 20:37 GMT
> Hello everybody.
>
[quoted text clipped - 7 lines]
>
> 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-)
Joey - 29 Jun 2006 02:40 GMT
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.
Patricia Shanahan - 29 Jun 2006 02:57 GMT
> 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.

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
Joey - 30 Jun 2006 02:51 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....
////////////////////////
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

:)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.