Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Getting the name of a variable?

Reply
Thread Tools

Getting the name of a variable?

 
 
Anon
Guest
Posts: n/a
 
      05-25-2004
This may sound stupid, but for debugging purposes I need to find out the
name of a variable.

e.g. if I have a variable called myVariable, how can I use Java to return
"myVariable" as a string?



 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      05-25-2004
Anon wrote:
> This may sound stupid, but for debugging purposes I need to find out the
> name of a variable.
>
> e.g. if I have a variable called myVariable, how can I use Java to return
> "myVariable" as a string?


Since variable names are known at compile time and not at runtime, the
correct answer is this:

String str = "myVariable"

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      05-25-2004
Anon wrote:
> This may sound stupid, but for debugging purposes I need to find out the
> name of a variable.
> e.g. if I have a variable called myVariable, how can I use Java to return
> "myVariable" as a string?


By using a real debugger.
 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      05-26-2004
"Anon" <> wrote in message news:<hpNsc.153$7A.96@newsfe1-win>...
> This may sound stupid, but for debugging purposes I need to find out the
> name of a variable.
>
> e.g. if I have a variable called myVariable, how can I use Java to return
> "myVariable" as a string?


It *does* sound stupid

If you know the variable name then you can just 'hard-code' the
string, why do you need to 'get' it at runtime?

If you don't know it (perhaps because it's in a separate package/class
you don't have source code for), then you can use reflection to query
a class for it's member names.

- sarge
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
print("my name is {name}, and {age}-year old {gender}", name, age, gender); =?iso-8859-1?B?bW9vcJk=?= Java 7 01-02-2006 04:39 PM
IE name="name" & form.name property bug Java script Dude Javascript 5 06-30-2004 03:07 AM
name = name.substring(0, name.lastIndexOf('.')); Help please Jack-2 Javascript 3 12-24-2003 04:39 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57